| | |
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: 977
Reputation:
Solved Threads: 273
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: 977
Reputation:
Solved Threads: 273
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 |
accessdenied advanced aliased apache application argv beginner bits calling casino change clear command convert corners count csv cturtle cursor def definedlines dynamic dynamically edit event events file float format frange function google homework i/o iframe inches input jaunty keyboard lapse line linux list lists loop matching microphone mouse movingimageswithpygame multiple newb number numbers numeric obexftp output parameters parsing path prime programming projects py py2exe pygame pyopengl python random rational raw_input recursion remote return reverse session signal software sprite statictext string strings syntax tails text threading time tlapse tuple ubuntu unicode unit urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython





