| | |
Ask user for binary number
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
Hi I am trying to ask the user to input a binary number and checking to see if
the input contains 0's and 1's. If not, I want to inform the user that the input was invalid and ask them to retry.
Any help to fix my code would be greatly appreciated.
the input contains 0's and 1's. If not, I want to inform the user that the input was invalid and ask them to retry.
Any help to fix my code would be greatly appreciated.
Python Syntax (Toggle Plain Text)
binary = int(raw_input("Enter a binary number:")) for i in range(binary): if (binary != 0 or binary != 1): print "Invalid input" binary = int(raw_input("Enter a binary number:"))
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
Python Syntax (Toggle Plain Text)
binary = int(raw_input("Enter a binary number:")) for i in range(binary): print "for loop is testing", i, "in range", binary if (binary != 0 or binary != 1): print binary, "not equal to 0 or 1" binary = int(raw_input("Enter a binary number:")) else: print binary, "equal to 0 or 1"
Last edited by woooee; Nov 20th, 2008 at 9:20 pm.
You could use list comprehension as well
python Syntax (Toggle Plain Text)
binary = raw_input() if False in [f == '0' or f == '1' for f in binary]: print "Not Binary" else: print "Is Binary"
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
Python Syntax (Toggle Plain Text)
still_testing = True while still_testing: bin = raw_input("\nEnter a binary number: ") if len(bin): still_testing = False for num in bin: if int(num) not in [0, 1]: print "Invalid input for", num still_testing = True
![]() |
Similar Threads
- Binary Real to Decimal Algorithm (Python)
- decimal to binary in while loop..stuck (C++)
- binary to decimal problem (C++)
- User-Defined Function - part 2 (C++)
- adding numbers of binary output (C)
- Can someone please help me with Binary Addtion. (C++)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- CAn any1 tell me watz wrong in da code (C++)
- help with codes (C)
- Sequential search in java (Java)
Other Threads in the Python Forum
- Previous Thread: python config files zip
- Next Thread: Python Multimedia Framework
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






