Noob trying to learn Python, can anyone help with this problem?

Soldato
Joined
23 Jul 2009
Posts
14,129
Location
Bath
Hi, I'm working through some youtube tutorials and doing exercises I've found on the web to try and solidify the knowledge.

One of the exercises was to create a number guessing game, which was simple enough using a while loop, but when I tried to add extra if statements to the while loop it went a bit funny. Here's what I came up with:

Code:
answer=int(raw_input("I'm thinking of a number 1 and 50, have a guess!"))

while answer != 32:
    if answer>=25 and answer<35:
        print "You're getting very warm now!"
    elif answer<25 and answer>10:
        print "Too low"
    elif answer<= 10:
        print "You couldn't be further off"
    elif answer>= 35:
        print "Too high that time!"

    print " Guess again!"
    answer=raw_input("Guess a number between 1 and 50: ")
else: print "Well done!"
raw_input("Hit enter to close window")

It works as intended the first time, but after the first incorrect guess (if correct the loop breaks and you can close as intended), it just prints the "too high" result regardless of whether the guess was too low, correct, or too high. So, I'm a bit stumped.

I originally had them all as "if" statements not "elif", but it's the same result. Why does the loop always print the same answer after the first guess instead of starting over like I'd expected it to?
 
Last edited:
Put your code within
Code:
 tags to get the vBulletin to retain the indenting.  It's difficult to diagnose Python code without this.

A quick glance would suggest it may be because you're not putting int() around the second raw_input, so it's treating the number at text?[/QUOTE]

Legend! That was it. I had originally had all the answers in string format, but then realised it was unnecessary and added the int to the first raw_input, but  forgot to add it to the last one. 

Works fine now. I thought I'd misunderstood how loops work, but it was just a silly mistake in a pointless program lol.
 
Good shout, those will be my next port of call. So far I came up with this, where a and b are the upper and lower values and answer is now the answer, programmable by the asker. The ranges are pretty primitive, but I wanted to make sure it worked before putting too much time into them! I'm not sure what to do about letters and symbols being entered by the user though, as that breaks when it hits the int() function?
Code:
a=1
b=500
answer=320

rangewaytoolow=round(answer/4)
rangelow= round(answer/2)
rangehigh=round((b-answer)/2)+answer
rangewaytoohigh=round((b-answer)/2)+answer
rangecloselower=answer-(round(b*0.05))
rangeclosehigher=answer+(round(b*0.05))
guess=int(raw_input("I'm thinking of a number between " +str(a)+ " and "+ str(b)+ ", have a guess!" ))

while guess != answer:
    if guess>=rangecloselower and guess<rangeclosehigher:
        print "You're getting very warm now!"
    elif guess<rangecloselower and guess>rangewaytoolow:
        print "Too low"
    elif guess<= rangewaytoolow:
        print "You are miles too low!"
    elif guess>=rangeclosehigher and guess<rangehigh:
        print "Too high that time!"
    elif guess>=rangewaytoohigh:
        print "miles too high!"
    print " Guess again!"
    guess=int(raw_input("Guess a number between " +str(a)+ " and "+ str(b)+": " ))
print "Well done!"
raw_input("Hit enter to close window")

Now, however, I have to cook dinner for my other half :/
 
Back
Top Bottom