Re: [Tutor] Binary/Decimal convertor
On 11/01/13 21:51, Ghadir Ghasemi wrote: Hi, I made a program called binary/denary convertor. Can anyone tell me about how I could stop the user entering a binary > number with more than 8 numbers or 8 bit Why would you want to? Your code can handle much bigger binary numbers, why limit the user to 8 bits? Here is the code. I started off by entering 'len' function. num1 = int(input('please enter the first 8 bit binary number: '),2) Here hyou read the number as a string and already convert it to a real number in Python. So the conversion from binary has already happened regardless of how long the string was. if len(num1) > 8: print("please enter an 8 bit binary number") You now try to see how long the number is, but numvers don;t have a length. So to do what you claim you want you need to do the len() check before you do the inty() conversion. Something like: num1 = input('please enter the first 8 bit binary number: ') if len(num1) > 8: print("please enter an 8 bit binary number") else: num = int(num,2) Then put that in while a loop that repeats until the length is <=8 return int(num1,2) And here you return which would terminate your function except you don't have a function so it will give a syntax error. And trying to convert a num as a binary won't work because its already stored internally as a binary number. This only works if you still have the string as described above. num2 = int(input('please enter the second 8 bit binary number: '),2) And this doesn't do any len() checks result = add_binary_numbers(num1, num2) print('the result is', bin(result)[2:]) I'd suggest that you make your add_binary_numbers() function return the binary string. Otherwise it doesn't add any value over the standard plus sign... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary/Decimal convertor
On 01/11/2013 04:51 PM, Ghadir Ghasemi wrote: > Hi, I made a program called binary/denary convertor. Can anyone tell me about > how I could stop the user entering a binary number with more than 8 numbers > or 8 bit by repeating the question over and over until they do enter a > correct binary number( 8-bit or less) > Here is the code. I started off by entering 'len' function. > > def add_binary_numbers(num1, num2): > return num1 + num2 > > num1 = int(input('please enter the first 8 bit binary number: '),2) > if len(num1) > 8: > print("please enter an 8 bit binary number") > return int(num1,2) > num2 = int(input('please enter the second 8 bit binary number: '),2) > result = add_binary_numbers(num1, num2) > print('the result is', bin(result)[2:]) > > 1. Specify the Python version. I'll assume version 3.3 2. Show whatever error you got. I'd guess something like the following: File "", line 1, in TypeError: object of type 'int' has no len() The len() function operates on strings, lists, and other sequences and mappings. But although you had a string at first, you've already converted it to an int, which has no len() method. It might be simplest to compare the value num to 0 and 255. Alternatively, you could compare it before doing the int function. Since you're doing it twice, it probably makes sense to do this in a function. Write a function that repeatedly asks the user till it gets a value in the right range. The other error that jumps out is that you have a return statement at top-level, while the statement only makes sense inside a function. That problem goes away once you put the input logic inside a function. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary/Decimal convertor
* Ghadir Ghasemi [2013-01-11 21:51]: > Hi, I made a program called binary/denary convertor. Can anyone tell > me about how I could stop the user entering a binary number with more > than 8 numbers or 8 bit by repeating the question over and over until > they do enter a correct binary number( 8-bit or less) Here is the > code. I started off by entering 'len' function. > > def add_binary_numbers(num1, num2): > return num1 + num2 > > num1 = int(input('please enter the first 8 bit binary number: '),2) > if len(num1) > 8: > print("please enter an 8 bit binary number") > return int(num1,2) > num2 = int(input('please enter the second 8 bit binary number: '),2) > result = add_binary_numbers(num1, num2) > print('the result is', bin(result)[2:]) It looks like the first thing you need to do is figure out how to use a loop (eg, while loop, for loop). I would recommend thinking about some pseudocode to determine your program's flow and then try to build something to accomplish that. -- David Rock da...@graniteweb.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor