[Tutor] guess my number game (reversed)

Peter Otten __peter__ at web.de
Sat Oct 6 09:24:18 EDT 2018

chiara pascucci wrote:

> Hi,
> sorry for "resurrecting" this thread. I have tried doing as suggested but
> with no luck.
> I now managed to make the programme work "partially". It works as desired
> for "too high" and "right" answer input, but when the users input "too
> low" the programme closes.
> here is what the code looks like right now.
> print ("Think of a number from 1 to 100, and I will try to guess it")
> input ("press enter when ready...")
> import random
> number = random.randint(1,100)
> print (number)
> answer = input ("right, too low, too high?")
> while answer != "right":
>     if answer == "too high":
>         number2 = random.randint (1,number)
>         print (number2)
>     elif answer == "too low":
>         number3 = random.randit (number,100)
>         print (number3)
>     answer = input ("is this right, too high, or too low?")
> print ("I finally guessed your number")
> input ("\n\n press the enter key to exit")
> any help is really appreciated :)
> Chiara

When you  run your script from the command line it not just terminates, it 
also prints an error message and important debug information (called 
"traceback") that helps you fix the code:

$ python3 guess_number_chiara.py 
Think of a number from 1 to 100, and I will try to guess it
press enter when ready...
right, too low, too high?too low
Traceback (most recent call last):
  File "guess_number_chiara.py", line 17, in <module>
    number3 = random.randit (number,100)
AttributeError: 'module' object has no attribute 'randit'

The problem is in line 17 and there's a module that doesn't have a randit 
attribute. The only module mentioned in the line is random which indeed does 
not contain a randit() function, the actual function is named randint().

So the bug turns out to be a misspelt function name.

More information about the Tutor mailing list