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

Il giorno ven 1 giu 2018 alle ore 18:15 Alan Gauld via Tutor <
tutor@python.org> ha scritto:

> On 01/06/18 14:00, chiara pascucci wrote:
>
> > the user's input. The programme works fine if the it guesses the number
> > right on its first try, but when the users inputs "too low" or "too high"
> > an infinite loop is returned. I thinkI have done something wrong in my
> > while loop and that my sentry variable is somehow wrong
>
> Nope, it's simpler than that.
>
> You only ask the user for input once, outside the loop.
> Move the input() statement inside the loop before the if
> statements and all should be well. (You will also need to
> initialise answer to some value - an empty string
> say - before entering the while loop.)
>
> > print("think of a number from 1 to 100")
> > print("I will try and guess it!")
> >
> > import random
> >
> > number = random.randint(1,100)
> > print(number)
> >
> > answer = input("is this right, too high, or too low?")
> >
> > while answer != "right":
> >     if answer == "too high":
> >         number2 = random.randint(1,number)
> >         print(number2)
> >     elif answer == "too low":
> >         number3 = random.randint(number,100)
> >         print(number3)
> >
> > print("I finally guessed your number. Thank you for playing")
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to