首页 > 解决方案 > 当被问及是否要重试时,用户回答“是”后,如何让该功能以新值重新启动?

问题描述

更新- 我试图在每次运行游戏后更新分数,所以基本上当用户或计算机获胜时,分数应该更新,但它只更新一次。重新尝试后如何让 userwin 和 compwin 保持更新?

**已解决 **我需要在游戏结束后重置计算机选项,并且它必须在 REPLAY 功能中。该代码仅重新启动函数而不获取新值。

import random

def retry():
    while True:
        replay = input("Want to try again?").lower()
        if replay not in "yn":
            print("Please enter Yes or No.")
        elif replay == "n" or replay == 'no':
            print("Thanks for playing.\n Bye now!")
            break
        elif replay == "y" or replay == "yes":
            print("Let's try again!")
            return rps()

def rps():
    while True:
        compwin = 0
        userwin = 0
        computer = random.choice(picks)
        user = input("Choose: Rock, Paper, Scissors: \n").lower()
        if user not in ['rock','paper','scissors']:
            print("Please choose a valid option.")
        if user == computer:
            print("It's a draw.")
            return retry()
        if ((user == 'rock' and computer == 'paper')or
            (user == 'paper' and computer == 'scissors')or
            (user == 'scissors' and computer == 'rock')):
                print("Computer won")
                compwin += 1
                print("Computer won: ",compwin,"games")
                return retry()
        if ((user == 'scissors' and computer == 'paper')or
              (user == 'paper' and computer == 'rock')or
              (user == 'rock' and computer == 'scissors')):
                print('You won!')
                userwin += 1
                print("User won: ",userwin,"games")
                return retry()


#Pay attention to wording and parantheses and where you add the line of codes: choice/choices()
picks = (['rock','paper','scissors'])
rps()

标签: python-3.xloopswhile-loop

解决方案


推荐阅读