Ask user for binary number

Thread Solved

Join Date: Sep 2008
Posts: 9
Reputation: pyth0n is an unknown quantity at this point 
Solved Threads: 0
pyth0n pyth0n is offline Offline
Newbie Poster

Ask user for binary number

 
0
  #1
Nov 20th, 2008
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.

  1. binary = int(raw_input("Enter a binary number:"))
  2. for i in range(binary):
  3. if (binary != 0 or binary != 1):
  4. print "Invalid input"
  5. binary = int(raw_input("Enter a binary number:"))
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Ask user for binary number

 
0
  #2
Nov 20th, 2008
  1. binary = int(raw_input("Enter a binary number:"))
  2. for i in range(binary):
  3. print "for loop is testing", i, "in range", binary
  4. if (binary != 0 or binary != 1):
  5. print binary, "not equal to 0 or 1"
  6. binary = int(raw_input("Enter a binary number:"))
  7. else:
  8. print binary, "equal to 0 or 1"
Last edited by woooee; Nov 20th, 2008 at 9:20 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 899
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Ask user for binary number

 
0
  #3
Nov 20th, 2008
You could use list comprehension as well
  1. binary = raw_input()
  2. if False in [f == '0' or f == '1' for f in binary]:
  3. print "Not Binary"
  4. else:
  5. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Ask user for binary number

 
0
  #4
Nov 21st, 2008
  1. still_testing = True
  2. while still_testing:
  3. bin = raw_input("\nEnter a binary number: ")
  4. if len(bin):
  5. still_testing = False
  6. for num in bin:
  7. if int(num) not in [0, 1]:
  8. print "Invalid input for", num
  9. still_testing = True
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Ask user for binary number

 
0
  #5
Nov 30th, 2008
Not to be too terribly picky, but the code in the above post where it uses if int(num) not in [0,1]: will fail if the user enters non-digit characters.

If the test were performed with strings it would not fail: if num not in ['0','1']:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC