Ask Question
16 May, 16:22

Using a WHILE loop write a program that asks the user to guess your favorite number. The program should end when the user guesses correctly, but otherwise provides hints for guessing higher or lower. Use the Try/Except/Else structure to ensure that the user enters numbers. Name this program guessNumber. py

+5
Answers (1)
  1. 16 May, 19:08
    0
    fav_number=26

    guess=0

    while 0==0:

    try:

    guess=int (input ('Enter your guess: '))

    except:

    print ('You have entered an invalid value.')

    else:

    if guess = = fav_number:

    break

    elif guess>fav_number:

    print ('Try a smaller number.')

    elif guess
    print ('Try a larger number')

    Explanation:

    define a variable fav_number and set it to number. write an infinite while loop to read input from user. In try block get input from user, if user enters invalid input code in except block will be executed. For valid input

    check if its greater than favorite number, if yes print try smaller number. check if its smaller than favorite number, if yes print try larger number. check if its equal to favorite number, break loop and exit
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Using a WHILE loop write a program that asks the user to guess your favorite number. The program should end when the user guesses ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers