# 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)
在代码底部询问玩家是否想再次玩的地方我遇到了问题。如果用户第一次输入“否”,它会按预期执行并退出游戏功能并打印再见图像。但是,如果用户输入“是”,它会重播游戏,这很好。但是,当它再次运行游戏并询问用户是否想再次玩时,他们说不,它不起作用。它只是询问您是否想再玩一次。
它似乎与用户输入“是”有关。 IE。如果用户第一次说“不”,那么第一次就可以正常工作。如果用户说是。然后你必须说“不”两次才能下次成功。如果用户连续两次说“是”并玩了几次游戏,那么您无需输入大量时间即可运行。
期望代码能够正常工作,以便如果用户输入“否”,则会退出游戏功能。目前只有当您不立即输入时才会执行此操作。如果你先说“是”,然后再玩一次再选择“否”,这是不行的。
游戏重新启动是通过递归调用完成的,但调用后它只会返回循环而不退出。
通话时或通话后返回应该可以:
if play_again == "yes":
return main_game(user_score, playing_again=True, option_3=option2)