0

I am using Python 2.7. The program generates a random number and asks the user to guess what it is. The while statements work good. The conditional if statement ends the program without following instructions of print followed by calling the function to see if the user wants to play again.

What makes an if statement not follow instructions? Is there a conflict with the later while statements?

# generate random number between 1 & 9
# have user guess the number, then
# tell them if they guessed too low,
# too high, or exactly right
# keep the game going until user types "exit"
# track how many guesses the user has taken, and when game ends, print it out

import random
a = random.randint(1, 9)

#def whatUp():
    #print ("You got it correct")

def playAgain():
    wieder = input("Do you wish to play again? Y or N ")
    if wieder == "Y":
        guessThis()
    elif wieder == "N":
        print ("Have a day")
    elif wieder != "Y" or "N":
        wieder = input("Do you wish to play again? Y or N ")


def guessThis():
    #a = random.randint(1, 9)
    findout = int(input("Enter a number from 1 to 9 "))
    i = 1

if findout == a:
    #whatUp()
    print ("You got it correct")
    playAgain()

while findout > a:
    print ("too high")
    findout = int(input("Enter a number from 1 to 9 "))
    i += 1

while findout < a:
    print ("too low")
    findout = int(input("Enter a number from 1 to 9 "))
    i +=1

#while findout != a:
    #print ("Incorrect")
    #findout = int(input("Enter a number from 1 to 9 "))
    #i += 1




guessThis()
2
  • Is this your exact indentation? I would expect if findout == a to be inside guessThis. Commented Nov 19, 2015 at 20:08
  • Is the indenting as intended? It looks like guessThis() is a 2 line function definition, and the ifs and whiles would be executed sequentially before the guessThis() is called. Commented Nov 19, 2015 at 20:11

3 Answers 3

1

Two issues (might be more):

  1. wieder != "Y" or "N": you can't do that, you probably meant to do: wieder not in ["Y", "N"]:

  2. When you declare findout inside a function - it will not be recognized outside. If you want it to be accessed from the outside - create it outside and pass it to the function, or alternatively, make the function return the value that you want back to the caller. Same goes for i.

Comment: regards #1, since you already checked both for 'Y' and 'N', the last condition can be modified from elif wieder != "Y" or "N": to a simple else

Sign up to request clarification or add additional context in comments.

Comments

0
    import random
    a = random.randint(1, 9)

    #def whatUp():
        #print ("You got it correct")

    def playAgain():
        wieder = raw_input("Do you wish to play again? Y or N ")
        if wieder == 'Y':
            guessThis()
        elif wieder == 'N':
            print ("Have a day")
        elif wieder != 'Y' or 'N':
            wieder = input("Do you wish to play again? Y or N ")


    def guessThis():
        #a = random.randint(1, 9)
        findout = int(input("Enter a number from 1 to 9 "))
        i = 1

        if findout == a:
            #whatUp()
            print ("You got it correct")
            playAgain()

        if findout > a:
            print ("too high")
            guessThis()
            i += 1

        if findout < a:
            print ("too low")
            guessThis()
            i +=1

    #while findout != a:
        #print ("Incorrect")
        #findout = int(input("Enter a number from 1 to 9 "))
        #i += 1




    guessThis()

Comments

0

Replace guessThis() and everything after with this:

def guessThis():
    findout = int(input("Enter a number from 1 to 9 "))
    i = 1

    #Keep going until win condition reached
    while findout != a:

        #too high guess
        if findout > a:
            print ("too high")  

        #too low guess
        if findout < a:
            print ("too low")

        #get next guess
        findout = int(input("Enter a number from 1 to 9 "))
        i += 1

    #We got out of the while, so the game is done :)
    print ("You got it correct")
    playAgain()

guessThis()

As yours is currently it will not work if the user guesses too high and then too low.

The main problem was that none of your code was executed in the right order cause your indents were off. You need to put the checking logic in the guessThis() function.

Also there is are issues on this function:

def playAgain():
    wieder = input("Do you wish to play again? Y or N ")
    if wieder == "Y":
        #need to reset a when playing again
        a = random.randint(1, 9)
        guessThis()
    elif wieder == "N":
        print ("Have a day")
    #because we have already checked (Y || N), simple 'else' gives us (!Y && !N)
    else:
        wieder = input("Do you wish to play again? Y or N ")

The !"Y" or "N" doesn't work quite like you expect it does, and I assume you want a new value of a for a new game

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.