I am writing a script that:
- Asks the user to enter a number.
- Detects errors of type 'ValueError'. The user has only 5 attempts to enter correct input.
- If the user types in zero, the script stops execution with the message 'All done'.
- If the user enters a positive integer, the script tells if the number is even or odd. It continues asking the user to type in another number; 10 times in total.
Here is my attempt.
attempt = 0
max_attempts = 5
i = 0
i_max = 10
while attempt < max_attempts:
attempt = 1
try:
while i < i_max:
i = 1
number = int(input('Please type in a positive integer; 0 to quite: '))
if number == 0:
print('All done!')
break
if number <0:
print('Please type in a positive number.')
if number%2 == 0:
print("The number",number, "is even.")
else:
print("The number",number, "is odd.")
print('Only', i_max-i, 'numbers left.')
except ValueError:
print('Wrong input, ', max_attempts - attempt, 'attempts left')
if attempt == max_attempts: # You tried too many times
print('Sorry, too many attempts!')
If the user enters 0 the script does not cease as desired. If the user enters correct input the final string 'Sorry, too many attempts!' is printed which is not desired in this case. How I can solve these issues? Any help will be greatly appreciated. Thank you very much in advance. I apologize if I am missing something rather elementary.
CodePudding user response:
If the user enters 0, then you break the current while loop. However, this while loop is in another while loop. You can add a variable "is_finish". If the user enters 0, set this new variable to True. In the first while loop condition, add "is_finish == False".
Hope this help you !
CodePudding user response:
You can use a for
loop to control the total number of repeats the user is allowed, then a separate failure
variable to count the number of times they enter something invalid:
max_success = 10
max_failures = 5
failure = 0
for attempt in range(max_success):
user_input = input(f' {attempt 1}/{max_success} - Ener a positive integer or 0 to quit: ')
if user_input == '0':
print(' Quit')
break
try:
user_input = int(user_input)
if user_input % 2 == 0:
print(' Even number')
else:
print(' Odd number')
except ValueError:
failure = 1
print(f' Not a number (failure {failure} of {max_failures})')
if failure >= max_failures:
break
Here's an example output if they submit 5 invalid entries:
1/10 - Ener a positive integer or 0 to quit: 5
Odd number
2/10 - Ener a positive integer or 0 to quit: a
Not a number (failure 1 of 5)
3/10 - Ener a positive integer or 0 to quit: b
Not a number (failure 2 of 5)
4/10 - Ener a positive integer or 0 to quit: c
Not a number (failure 3 of 5)
5/10 - Ener a positive integer or 0 to quit: d
Not a number (failure 4 of 5)
6/10 - Ener a positive integer or 0 to quit: e
Not a number (failure 5 of 5)
CodePudding user response:
add attempt = 6 inside the if condition (if number == 0)
attempt = 0
max_attempts = 5
i = 0
i_max = 10
while attempt < max_attempts:
attempt = 1
try:
while i < i_max:
i = 1
number = int(input('Please type in a positive integer; 0 to quite: '))
if number == 0:
print('All done!')
attempt = 6
break
if number < 0:
print('Please type in a positive number.')
if number % 2 == 0:
print("The number", number, "is even.")
else:
print("The number", number, "is odd.")
print('Only', i_max - i, 'numbers left.')
except ValueError:
print('Wrong input, ', max_attempts - attempt, 'attempts left')
if attempt == max_attempts: # You tried too many times
print('Sorry, too many attempts!')