Skip to content
Advertisement

Write a Python program to guess a number between 1 to 9

Write a Python program to guess a number between 1 to 9 Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears again until the guess is correct, on successful guess, user will get a “Well guessed!” message, and the program will exit.

The above statement is given.. i have written code but it hangs after giving input.. after ctr+c it shows last call on if statement.

JavaScript

Advertisement

Answer

OP:

i have written code but it hangs after giving input.. after ctr+c it shows last call on if statement.

Because:

Imagine you’ve entered an incorrrect number, the condition fails, the loop continues, it checks the same condition and fails again, keeps going on and on.

What you need:

  1. Put the ug = int(input("Guess a number")) inside the while loop.

  2. Put an else block with a Incorrect Guess prompt message.

Hence:

JavaScript

OUTPUT:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement