adamgeo1
adamgeo1

Reputation: 53

My Code isn't Giving Me the Expected Outcome in Python

(Codehs has inbuilt Turtle) I'm using codehs.com for a class in school and we're currently working on turtle graphics. These simple if/elif/else statements aren't reacting correctly to inputted numbers. They're supposed to draw a down arrow if the user number is above the secret number (4), and a up arrow if its below the secret number. When the user inputs a number that isn't the secret number, it displays either arrow and also reopens the input for the user. If the number is correctly guessed it displays a check mark.

I tried researching my problem and I couldn't find anything related to my specific problem.

    user_number = int(input("Choose a number between 1 and 10: "))
    secret_number = 4
    def checkmark():
        color("green")
        pensize(8)
        penup()
        left(45)
        forward(50)
        pendown()
        backward(50)
        left(90)
        forward(25)
    def down_arrow():
        penup()
        setposition(0,-25)
        pendown()
        left(90)
        forward(50)
        right(45)
        backward(25)
        forward(25)
        left(90)
        backward(25)
    def up_arrow():
        penup()
        setposition(0,25)
        pendown()
        right(90)
        forward(50)
        right(45)
        backward(25)
        forward(25)
        left(90)
        backward(25)
    while user_number != secret_number:
        user_number = int(input("Choose a number between 1 and 10: "))
    if user_number ==secret_number:
            checkmark()
    elif user_number < secret_number:
        up_arrow()
        user_number = int(input("Choose a number between 1 and 10: "))
    else:
        down_arrow()
        user_number = int(input("Choose a number between 1 and 10: "))

It should display either a up arrow or a down arrow depending if the typed number is higher or lower than the secret number, but it skips the arrows and just goes right back to the input box.

Upvotes: 1

Views: 272

Answers (1)

Swimmer F
Swimmer F

Reputation: 903

the If clauses after the while loop are not correctly indented, your while loop is just

while user_number != secret_number:
    user_number = int(input("Choose a number between 1 and 10: "))

and the only way to get out of the loop is to get the secret number correct - at which point the if statement is true, checkmark() is run and the program ends

To fix the error, just indent the if and else clauses.

Upvotes: 1

Related Questions