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

Soldato
Joined
23 Jul 2009
Posts
14,128
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:
I have never programmed in Python, so hopefully someone will respond who has.

Is the input (answer variable) being reset? It sounds like it is adding it to make it more than 35.

Just an idea :)
 
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?  The structures aren't quite right either.  Corrected it below, if you want to look, but I'd encourage you to fault find it yourself.

[spoiler][code]
 
 answer=int(raw_input("I'm thinking of a number 1 and 50, have a guess!"))
 
 while answer != 32:
    # This part is conditional
    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!"
    # This is always executed, as it's outside the if structure
    print "Guess again!"
    answer=int(raw_input("Guess a number between 1 and 50: ")) # Added int()
 print "Well done!" # Removed else, as the only time this needs to be  executed is when the while loop is broken - ie the ans is 32.
 raw_input("Hit enter to close window")
[/spoiler]
 
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.
 
See my edit above also, though. The structures aren't quite right (depending on how everything was indented, of course).

Works fine now. I thought I'd misunderstood how loops work, but it was just a silly mistake in a pointless program lol.
There's a point to it if it helps you learn. How about some extensions to it:

what if I put in a number outside 1-50?
what if I write 'african swallow' at the prompt instead of a number?
what if I didn't want to always be guessing 32? Set it up so that you can input a number for someone else to guess - you'll have to then use calculations to give you the ranges for the hints ...

chop chop :p
 
Last edited:
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