-1

Hey there so i have my number guesser in python3.But i want to give out an Error if the User types letters instead of numbers.When i type a letter it says

Traceback (most recent call last):
  File "nummerlol.py", line 14, in <module>
    guess = int(guess)
ValueError: invalid literal for int() with base 10:"

Is there a way to change this error? thx

import time
import random 
from secrets import randbelow
guesscount = 0
print("Hey whats your name")
name = input()
number = randbelow(20)
print("The Number is between 1 & 20")
while guesscount < 6:
    print("Guess.")
    guess = input()
    guess = int(guess)
    guesscount = guesscount + 1
    if guess < number:
        print("My Number is bigger")
    if guess > number:
        print("My Number is lower")
    if guess == number:
        break
if guess == number :
guesscount = str(guesscount)
print("Nice",name ,".")
print("You needed" ,guesscount ,"Tries")
if guess != number:
    number = str(number)
    print("Bruh,My Number was" ,number)`
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
bot_diyar
  • 80
  • 7

2 Answers2

0

A similar question to yours already exists, but I'll answer your question anyway.

As you already know, the reason you're getting a traceback response is because you're trying to convert a non-integer into an integer when you call int(guess). You can catch this exception using a try/except block. For example, you could replace the offending line of code with the following:

while True:
  try:
    guess = int(guess)
    break
  except ValueError:
    print("You must enter a valid number.")

This code will cause the program to require input from the user until that input is valid. Have a look at this Python3 handling errors and exceptions documentation for more information.

Tasha R
  • 33
  • 1
  • 7
  • If you identified this question as a duplicate then you shouldn't even answer - either vote to close the question or if you can't yet post a comment linking to the duplicate. – bruno desthuilliers Mar 30 '18 at 14:39
0

You could use a try-except loop. With continue (as below) the user can keep guessing. You could also put your own exception in place of the print and continue if you want the script to break with a more informative message.

import time
import random 
from secrets import randbelow
guesscount = 0
print("Hey whats your name")
name = input()
number = randbelow(20)
print("The Number is between 1 & 20")
while guesscount < 6:
    print("Guess.")
    guess = input()
    try:
        guess = int(guess)
    except:
        print("That is not a number")
        continue
    guesscount = guesscount + 1
    if guess < number:
        print("My Number is bigger")
    if guess > number:
        print("My Number is lower")
    if guess == number:
        break
if guess == number :
    guesscount = str(guesscount)
print("Nice",name ,".")
print("You needed" ,guesscount ,"Tries")
if guess != number:
    number = str(number)
    print("Bruh,My Number was" ,number)
RuthC
  • 1,269
  • 1
  • 10
  • 21