0

i'm writing a program asking the user to guess number which already generated automatically. the think that as per below code the user should to enter int number but what if the user enter "exit" to exit the program it will show an error due "exit" is string not int, how to solve this issue since i tried to find the solution but i did not find the proper answer

import random
import sys

def GuessedNumber():

    while(True):
        Generate = random.randint(1,9)

        GuessNumber = int(input("Dear user kidnly guess a number between 1-9: "))

        if GuessNumber == Generate:
            print("You guessed right number")

        elif GuessNumber > Generate:
            print("Guessed number is too high")

        elif GuessNumber < Generate:
            print("Guessed number is too low")



GuessedNumber()
1
  • Please be more specific, what issue are you facing? Also, variable and function names should follow the lower_case_with_underscores style. Commented Feb 8, 2020 at 21:30

2 Answers 2

2

Assign the input to a string, then check for exit, and only if it doesn't match then proceed with the conversion to an integer variable.

Sign up to request clarification or add additional context in comments.

4 Comments

@tripleee I don't see the point in giving full working code on something which is likely some kind of homework assignment.
Fine; edit reverted. I'm thinking if they couldn't figure it out on their own, mere prose is not enough. But I have rolled back the edit; I was a bit hesitant to edit someone else's answer in the first place.
@tripleee Thanks I'm more the teach to fish guy, than handing out fishes.
@tripleee thx for helping i used this way as per your suggestion and made some changes in code and not work perfect GuessNumber = input("Dear user kidnly guess a number between 1-9: ") if GuessNumber == "exit": sys.exit(0) else: if int(GuessNumber) == Generate: print("You guessed right number")
1

You can check the input is in valid range 1-9 or not. If the input is in the range then do what you were doing, else if the input is exit then exit the game otherwise say him to enter valid input and continue the loop. Here is the code:

import random

def GuessedNumber():
    while(True):
        Generate = random.randint(1,9)
        GuessNumber = input("Dear user kidnly guess a number between 1-9: ")
        if GuessNumber in list('123456789'):
            GuessNumber = int(GuessNumber)
            if GuessNumber == Generate:
                print("You guessed right number")
            elif GuessNumber > Generate:
                print("Guessed number is too high")
            elif GuessNumber < Generate:
                print("Guessed number is too low")
        else:
            if GuessNumber == 'exit':
                print('Good bye!')
                break
            else: # for any other non invalid input
                print('Please enter a valid input')
                continue

GuessedNumber()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.