Ex31 Problem with if - elif - else statements in my own script

Hi,

Been working my way through the LP3THW book…been fun so far and learning a lot.

Thought I was understanding ex31…if - elif - else statements. However, when I tried to create my own game using them, I realized that I’m not understanding how to get the flow right when getting user input.

When running script and entering the correct answer, I get another prompt after the print statement shows up, instead of script just stop running.

If I enter an incorrect choice, it doesn’t run the ‘second_guess’.

Also, need to create a ‘third_guess’ in case the ‘second_guess’ is incorrect.

I’d like to get this version of script to run correctly so that I learn/understand how to use if - elif - else properly.

After that, would like to create another version of this game as it seems this version is more complicated way to do this than necessary. Thinking there’s a simpler logic flow approach in that either user enters the right answer on the first try and then game should end or any wrong answer (ie, not 3…), should trigger the ‘second_guess’ to run and not a separate if -elif - else statement for each separate number entry.

Appreciate any help with this!

Mark

Here’s my current version of the game:

print("Guess my favorite musician or group")

print("You get 3 guesses to win a prize") print("Enter 1 for Elvis, 2 for Beatles, 3 for Stones, 4 for Led Zep, 5 for The Who.")

first_guess = input("> ")

if first_guess == "1":
    print("Sorry, not correct.")
    print("Try again.")

elif first_guess == "2":
    print("Sorry, try again.")

elif first_guess == "3":
    print("That's it! You get a prize!")
    print("Good job!")

elif first_guess == "4":
    print("Sorry, try again.")
 
elif first_guess == "5":
    print("Sorry, try again")


second_guess = input("> ")

    if second_guess == "2":
        print("Nice try, but incorrect. Try one more time.")
    elif second_guess == "3":
        print("Great! That's it! A prize for you!")
    elif second_guess == "4":
        print("Sorry, no prize for you")
    elif second_guess == "5":
        print("Nope, that's not right.")


Worked on this again…

Now working OK, except need to figure out how to stop program running, if someone enters correct answer.

Here’s what I wrote:

print("Guess my favorite group or musician.")

print("You get 3 guesses to win a prize”) print(“Enter 1 for Elvis, 2 for Beatles, 3 for Stones, 4 for Led Zep, 5 for The Who.")

first_guess = input("> ")

if first_guess == "1":
    print("Sorry, not correct. Try again.")

elif first_guess == "2":
    print("Sorry, try again.")

elif first_guess == "3":
    print("That’s it! You get a prize!")

elif first_guess == "4":
    print("Sorry, try again")

elif first_guess == "5":
    print("Sorry. try again.")

second_guess = input("> ")

if second_guess == "1":
    print("Nice try, but incorrect. Try one more time.")

elif second_guess == "2":
    print("Sorry, try one more time.")

elif second_guess == "3":
    print("Great! That's it! A prize for you!")

elif second_guess == "4":
    print("Sorry, no prize for you")

elif second_guess == "5":
    print("Nope, that's not right.")

third_guess = input("> ")

if third_guess == "1":
    print("Sorry, no more tries.")

elif third_guess == "2":
    print("Sorry, no more tries.")

elif third_guess == "3":
    print("You did it! You get a prize!")

elif third_guess == "4":
    print("Sorry, no more tries.")

elif third_guess == "5":
    print("Sorry, no more tries.")

Hi @ArchiMark. Firstly, good choice of bands :metal:

The if-elif-else flow provides you the ability to apply conditional behaviour as you have done. But perhaps consider the catch-all else statement too.

For example, you could save a lot of repetition by having:

if first_guess == “3”:
    print(“You win!)
    exit()
else:
    print(“Nope, try again”)

This way, you only have to specify what is right, and use a catch all for all the wrong examples.

I’ve added an exit() to stop the flow and exit the program if it’s right :wink:

You’ll find that when you hit ‘loops’ you can write your game much easier, so keep on plugging.

1 Like

Thanks for the good words and clear help! Greatly appreciate it!

Get what you’re saying and makes perfect sense to me…much more efficient, which is part of what I was struggling with…

Will keep on plugging away at it…

:grinning:

2 Likes

Glad @gpkesley helped you out. It looks like you’re used to another forum that does code like this:

`
# code here
`

In this forum, you do either three backticks:

```
# code here
```

Or, you use code brackets:

[code]
# code here
[/code]

Other than that good post.

Thanks, Zed, for the explanation…yes, you’re right, used to other forums…but now I get it…

Onward and updward…

No problem, there’s no standard really and honestly I wish the forum would catch code blocks and tell you this.