'How can I change this print() error message into an input() prompt?
How could I change the print statement to another input statement that says: "Try again. Your number must be between 1 and 50: " if num does not equal 1-50 or is not an integer?
while True:
num = input('Enter a whole number between 1 and 50: ')
try:
num = int(num)
if num<1 or num>50:
print("Your number must be between 1 and 50, try again.")
continue
break
except ValueError:
print("You did not enter a number, try again.")
Solution 1:[1]
Simply change the message after you used it once:
message = 'Enter a whole number between 1 and 50: '
while True:
num = input( message )
try:
# change the message
message = "Your number must be between 1 and 50, try again: "
num = int(num)
if 1 <= num <= 50:
break
except ValueError:
pass
Test:
Enter a whole number between 1 and 50: dont want to
Your number must be between 1 and 50, try again: 99
Your number must be between 1 and 50, try again: -2
Your number must be between 1 and 50, try again: 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
