0

Apparently this is a duplicate question but I did search before posting it and couldn't find anything that helped. I'm not aware of having asked it more than once.

First let me say that I'm a noob at this and so my jargon is not quite up to scratch so please be gentle if you feel that I haven't described my problem properly. Thank you.

I have written this script and it works perfectly:

import random

secret = random.randint(1,99)
guess = 0 
tries = 0

print ("You have six tries to guess the secret number between 1 and 99")

guess = int(input("What's first your guess?"))
while tries < 5 and guess != secret:
    if guess < secret:
        print ("Too low.")
        tries = tries + 1
        if tries == 1:
            guess = int(input("What's your second guess?"))
        if tries == 2:
            guess = int(input("What's your third guess?"))
        if tries == 3:
            guess = int(input("What's your fourth guess?"))
        if tries == 4:
            guess = int(input("What's your fifth guess?"))
        if tries == 5:
            guess = int(input("What's your sixth guess?"))


    elif guess > secret:
        print ("too high")
        tries = tries + 1
        if tries == 1:
            guess = int(input("What's your second guess?"))
        if tries == 2:
            guess = int(input("What's your third guess?"))
        if tries == 3:
            guess = int(input("What's your fourth guess?"))
        if tries == 4:
            guess = int(input("What's your fifth guess?"))
        if tries == 5:
            guess = int(input("What's your sixth guess?"))

if guess == secret:
    print ("you got it!")
else:
    print ('No more guesses. The secret number was', secret)

................................................................................ However when I define a function as follows:

def ordinal_guess():
    if tries == 1:
        guess = int(input("What's your second guess?"))
    if tries == 2:
        guess = int(input("What's your third guess?"))
    if tries == 3:
        guess = int(input("What's your fourth guess?"))
    if tries == 4:
        guess = int(input("What's your fifth guess?"))
    if tries == 5:
        guess = int(input("What's your sixth guess?"))

and call the function as follows:

while tries < 5 and guess != secret:
    if guess < secret:
        print ("Too low.")
        tries = tries + 1
        ordinal_guess()        
    elif guess > secret:
        print ("too high")
        tries = tries + 1
        ordinal_guess()

The script no longer wants to play nice, telling me the guess entered is always either too low or too high. see below:

You have six tries to guess the secret number between 1 and 99 What's first your guess?99 too high What's your second guess?1 too high What's your third guess?3 too high What's your fourth guess?5 too high What's your fifth guess?85 too high What's your sixth guess?50 No more guesses. The secret number was 81

clearly some of those numbers weren't too high.

Likewise here: You have six tries to guess the secret number between 1 and 99 What's first your guess?50 Too low. What's your second guess?99 Too low. What's your third guess?80 Too low. What's your fourth guess?70 Too low. What's your fifth guess?65 Too low. What's your sixth guess?53 No more guesses. The secret number was 75

What am I doing wrong?

sleepylog
  • 69
  • 1
  • 13
  • 1
    `guess` inside your function is a local variable, and assigning to it does not affect `guess` outside your function. – juanpa.arrivillaga Mar 16 '17 at 07:55
  • 1
    Try `guess = ordinal_guess()` outside and `return guess` from inside `ordinal_guess()` - as @juanpa.arrivillaga says, the 2 `guess` variables are not the same. Alternative you can have `guess` as global variable but in this case it is a bad practice... – urban Mar 16 '17 at 07:58
  • wow, I have no idea what any of that means lol. guess I'll just watch all those 100 video tutorials for beginners again. Thank you for your time though. – sleepylog Mar 16 '17 at 08:32
  • Actually, thanks urban, I worked out what it meant and it worked! – sleepylog Mar 16 '17 at 08:43

0 Answers0