Skip to main content
Remove bogus syntax highlighting. Remove extra characters, which I guess were supposed to be for formatting.
Source Link
wjandrea
  • 34k
  • 10
  • 69
  • 105

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print("{input} is not a number, please enter a number only".format(input=input_value)) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States.  Please enter your age: potato potato is not a number, please enter a number only Please enter your age: 5 You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print("{input} is not a number, please enter a number only".format(input=input_value)) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print("{input} is not a number, please enter a number only".format(input=input_value)) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

 Please enter your age: potato potato is not a number, please enter a number only Please enter your age: 5 You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Since the question has been edited to use python 3, the answers should be python 3 as well.
Source Link
Håken Lid
  • 23.2k
  • 10
  • 58
  • 73

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = raw_inputinput("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print ("{input} is not a number, please enter a number only".format(input=input_value)) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = raw_input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print "{input} is not a number, please enter a number only".format(input=input_value) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print("{input} is not a number, please enter a number only".format(input=input_value)) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Source Link
Steven Stip
  • 387
  • 3
  • 11

Why would you do a while True and then break out of this loop while you can also just put your requirements in the while statement since all you want is to stop once you have the age?

age = None while age is None: input_value = raw_input("Please enter your age: ") try: # try and convert the string input to a number age = int(input_value) except ValueError: # tell the user off print "{input} is not a number, please enter a number only".format(input=input_value) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 

This would result in the following:

Please enter your age: *potato* potato is not a number, please enter a number only Please enter your age: *5* You are not able to vote in the United States. 

this will work since age will never have a value that will not make sense and the code follows the logic of your "business process"

Post Made Community Wiki by Steven Stip