-2

When the program goes to level one, and the game begins, I input the correct answer, but it does not work. The answer to the first quote is "death". The if else statement is supposed to acquire the answer of the user, and, if it is correct, display a message and move on to the next level. However, it does not. It displays another message telling me I lost.

I don't understand what is going on, especially since the if else statement picks up whether or not the user wants to view the rules.

Just to be safe, I am going to write one last clarification in a more straightforward form.

When I input the correct answer on level one, and I know for a fact it is the correct answer, it does not display the congrats_message, but it displays the loss_message.

welcome_message = "Hello! Welcome to Quote Game by Chandler Morell!"
version = "0.0.1"

congrats_message = "Congratulations! You passed level one!"
loss_message = "Sorry. You did not succeed. One life has been lost."

win_game_message = "Congratulations! You won the game!"
lost_game_message = "You made it to the end, but you did not win. Give it another go!"

lives = 3


def game_won():
    print("")
    print(win_game_message)


def game_lost():
    print("")
    print(lost_game_message)


def update_lives():
    global lives
    lives = lives - 1

    if lives is 0:
        print("You have ran out of lives. Game over.")
        game_lost()
        exit(0)


def view_rules():
    rules = ["1. Do not look up quotes online.", "2. You have three lives.",
             "3. You will have three words to choose from.", "4. Type the WORD not the NUMBER"]
    print("")

    for i in rules:
        print(i)


def ask_view_rules():
    rules_answer = input("Would you like to view the rules? (y/n): ")
    if rules_answer is "y":
        view_rules()
    else:
        print("")
        print("Okay, let's begin!")


def level_one():
    print("Lives: ", lives)
    print("We will start with an easy quote. Fill in the blank.")
    print("")
    choices = ["1. death", "2. torture", "3. nothing"]

    for i in choices:
        print(i)
        print("")

    choice = input("Give me liberty or give me _____! - Patrick Henry ")

    if choice is "death":
        print("")
        print(congrats_message)
        print("")
        level_two()
    else:
        print("")
        print(loss_message)
        print("")
        update_lives()
        level_two()


def level_two():
    print("-----------------------")
    print("")
    print("Lives: ", lives)
    print("Welcome to level two!")
    print("")

    choices = ["1. stupidity", "2. wisdom", "3. nobody"]

    for i in choices:
        print(i)
        print("")

    choice = input("Knowledge speaks, but ______ listens. - Jimi Hendrix ")

    if choice is "wisdom":
        print("")
        print(congrats_message)
        print("")
        level_three()
    else:
        print("")
        print(loss_message)
        print("")
        update_lives()
        level_three()


def level_three():
    print("-----------------------")
    print("")
    print("Lives: ", lives)
    print("")
    print("Wow. I am impressed!")


print("")
print(welcome_message)
print("Version: " + version)

ask_view_rules()

print("")
print("We will now begin the game!")
print("")

level_one()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
SudoSu
  • 3
  • 4
  • Possible duplicate: https://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs – OneCricketeer Apr 08 '18 at 16:59
  • 1
    It kinda destroys the point of using functions if you define a new global in it every time it's called... – quantik Apr 08 '18 at 17:00
  • @quantik `global lives` doesn't redefine it. – OneCricketeer Apr 08 '18 at 17:02
  • 2
    @cricket_007 why define globals in functions to begin with? It's horrible practice in pretty much any situation – quantik Apr 08 '18 at 17:03
  • Thank you, sir. As a fifteen year old who is interested in programming, unlike many others, I have very few resources (as far as live people go). The solution worked. I'm sorry for the duplicate. – SudoSu Apr 08 '18 at 17:04
  • The official online python tutorial should have taught you `==` before `is`... And there's countless other resources to learn Python – OneCricketeer Apr 08 '18 at 17:06
  • https://docs.python.org/3/tutorial/ might help, also give [How to debug small programs (#2)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) a read and get/learn how to use a debugger - it will take some time now but spare you houres of guessing and posting questions in forums ;o) – Patrick Artner Apr 08 '18 at 17:08

1 Answers1

0

As cricket_007 pointed out you need to use == instead of is in your code. This is because is returns True if both variables point to the same object whereas == returns True if the value of both variables are equal. I would also suggest that you use a line break in your code \n as opposed to using print(“”).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
boi
  • 181
  • 1
  • 10
  • Thank you! I am a complete beginner at Python. I watched like six TheNewBoston tutorials on YouTube and decided to make this. Yeah. I knew that print("") probably wasn't the best way to do it. You live and you learn. – SudoSu Apr 08 '18 at 21:36