Using a while loop, write a code that asks the user "What is 7 multiplied by 5?". When the user answers the question incorrectly, ask them again but give them a hint on whether to go higher or lower. Continue to prompt for a response until they get the c orrect answer. However, if they do not get the answer after 5 tries, exit the loop and tell them the answer,

Respuesta :

Answer:

i=0

while i < 5:

   try:

       ans = int(input('What is 7 multiplied by 5: '))

       if ans != 35:

           raise ValueError

       else:

           break

   except:

       print('Enter correct value')

       print('Hint: 5 x 6 = 30 add 5 to get the corect answer')

   i=i+1

print()

print("The correct answer is 35")

Explanation:

The counter 'i' is initialized an to 0. The while loop is going to run as long as the counter is less than 5, i.e it will count from 0-4.

The Try Block is combined with the IF Statement to check the if the input is not equal to 35.

If the input is anything other than 35 the IF block is executed and a ValueError is raised and the program executes the Except Block. In the Except block, the program will prompt the user to try again and offer a hint to the user to help the user in solving the problem.

If the input is 35, the ELSE block is executed, the ELSE Block breaks the loop and prints the answer.

In a situation where the user enters 5 wrong inputs, the while loop ends and the Answer is printed to the screen.

I ran the code and attached the results for you to see how it works;

  • when given 5 wrong values.
  • 3 wrong values and 1 correct value

Ver imagen jehonor
Ver imagen jehonor