If / Elif / Else statement does not work as expected to exit game

Advertisements
# Reserved for our import statements.
import art
import gameData
import random


# Reserved for functions.
# main_Game() function holds the re-loopable code for our application.
def main_game(user_score, playing_again, option_3):
    user_score = user_score
    # Select 2 celebrities from our data list and display to use.
    random_num_1 = random.randint(1, 50)
    random_num_2 = random.randint(1, 50)
    while random_num_1 == random_num_2:
        random_num_2 = random.randint(1, 50)
    playing_again = playing_again
    if playing_again is True:
        option1 = option_3
        option2 = gameData.data[random_num_2]
    elif playing_again is False:
        option1 = gameData.data[random_num_1]
        option2 = gameData.data[random_num_2]

    option_1_name = list(option1.items())[0][1]
    option_1_followers = list(option1.items())[1][1]
    option_1_description = list(option1.items())[2][1]
    option_1_country = list(option1.items())[3][1]
    option_2_name = list(option2.items())[0][1]
    option_2_followers = list(option2.items())[1][1]
    option_2_description = list(option2.items())[2][1]
    option_2_country = list(option2.items())[3][1]

    # Ask user to guess which is correct.
    print(f"\nCompare A: {option_1_name}, a {option_1_description}, from {option_1_country}.")
    print(art.vs_image)
    print(f"Against B: {option_2_name}, a {option_2_description}, from {option_2_country}.")
    acceptable_input = False
    while acceptable_input is False:
        user_guess = input("\nWho has more followers? Type 'A' or 'B': ")
        user_guess = user_guess.upper()
        if user_guess == "A" or user_guess == "B":
            acceptable_input = True
        else:
            print("You can only enter 'A' or 'B'. Please try again.")
            acceptable_input = False

    # Confirm to user if they were correct and display their current score.
    correct_answer = ""
    if option_1_followers > option_2_followers:
        correct_answer = "A".upper()
    elif option_2_followers > option_1_followers:
        correct_answer = "B".upper()

    if user_guess == correct_answer:
        print("\nWell done you were correct!")
        user_score += 1
        print(f"You're score is now {user_score}")
    elif user_guess != correct_answer:
        print("\nYou got it wrong!")

    while True:
        play_again = input("\nDo you want to play again? (yes or no): ")
        play_again = play_again.lower()
        if play_again == "yes":
            main_game(user_score, playing_again=True, option_3=option2)
        elif play_again == "no":
            return 0
        else:
            print("You have not entered 'yes' or 'no'. Please try again.")


# Reserved for code that runs at beginning of application.

# Introduce user to the game.
print(art.starting_image)
print("Welcome to the higher or lower game!")
print("\nGame Rules: From the 2 options shown you have to pick which celebrity or brand has more Instagram followers.")

# Launch our game by calling the main_game() function. We send through the argument 0 which is the users starting score.
status = main_game(0, False, "")

if status == 0:
    print(art.exit_image)

Near the bottom of the code where it asks the player if they want to play again I am running into an issue. If the user inputs ‘no’ the first time it does as expected and exits the game function and prints the goodbye image. However if user inputs yes it replays the game which is fine. But when it runs through the game again and asks the user if they want to play again and they say no it doesnt work. It just asks you if you want to play again a further time.

It seems connected to the user inputting ‘yes’. ie. If user says no the first time it works fine first time. If user says yes. You then have to say no twice for it to work next time. If user says yes twice in a row and plays through the game a few times you then need to input no loads of times for it to work.

Expecting the code to work so that if user inputs no it will exit the game function. Which it currently only does if you enter no straight away. It wont work if you say yes first and then play again before selecting no.

>Solution :

The game restart is done using a recursive call, but after the call it will just go back to the loop without exiting.

Returning on or after the call should work:

        if play_again == "yes":
            return main_game(user_score, playing_again=True, option_3=option2)

Leave a ReplyCancel reply