开发者社区> 问答> 正文

轮#计数器不会重置时,重新启动游戏&当用户输入无效

所以我不明白为什么我的计数器不重置时,我重新开始游戏,而且,我相信这与我没有建设者的事实有关? 任何帮助将是高度赞赏!

import random
import sys
from termcolor import colored

class RPS():

    def show_welcome():
        print(colored("Welcome to RPS !!!", 'blue'))
        print(colored("RULES:",'yellow'), colored("*", 'red'), "Rock Beats Scissors",colored("*", 'red'))
        print(colored("       *", 'grey'),"Scissors beats Paper", colored("*", 'grey'))
        print(colored("       *", 'magenta'), "Paper beats Rock", colored("*", 'magenta'))
        print(colored("!---=== GOOD LUCK ===---! ", 'green'))

    def round_number():
        x = 10
        user_input = ""

        while True:
            try:
                user_input = int(input("# Number Of Rounds? [Max 10]: "))
            except:
                print("Invalid Input!")
                RPS.round_number()

            if user_input < 1 or user_input > x:
                print("Max 10 rounds!")
                rundnum = 0
                RPS.round_number()
            else:
                return user_input

    def restart():
        user_input = ""
        try:
            user_input = input("Would you like to restart? [Y/n]")
        except:
            print("\nInvalid Input! ")
            RPS.show_welcome()

        if user_input is "y" or user_input is "Y":
            rounds = 0
            RPS.round_number()
        elif user_input is "n" or user_input is "N":
            print(colored("Thanks for playing! Goodbye!", 'green'))
            sys.exit()
        else:
            print("Bullshit input!")
            RPS.restart()

    def game():

        player = 0
        rounds = 0
        moves = ['Rock', 'Paper', 'Scissors',]
        r = rundnum

        while rounds < r:
            rounds += 1
            comp = random.choice(moves)

            print(colored("Choose : ", 'green'), colored("1)Rock", 'red'), colored("2)Paper", 'yellow'),
                  colored("3)Scissors", 'grey'))

            try:
                player = int(input("What's Your Guess? "))
            except:
                print(colored("No Valid Input! Restarting Game...", 'red'))
                RPS.game()

            if (player is 1 and comp is 'Rock') or (player is 2 and comp is 'Paper') \
                or (player is 3 and comp is 'Scissors'):

                print("Player Choose: {}".format(moves[player - 1]))
                print("Computer Choose: {}".format(comp))
                print(colored("*** Round #: {} | Result: It's A Draw !! *** ", 'blue').format(rounds))

                if rounds >= r:
                    print(colored("*** GAME OVER *** ", 'grey'))
                    rounds = 0
                    RPS.restart()
                else:
                    continue

            elif (player is 1 and comp is 'Paper') or (player is 2 and comp is 'Scissors') \
                or (player is 3 and comp is 'Rock'):

                print("Player Choose: {}".format(moves[player - 1]))
                print("Computer Choose: {}".format(comp))
                print(colored("*** Round #: {} | Result: Player Lose !! *** ", 'blue').format(rounds))

                if rounds >= r:
                    print(colored("*** GAME OVER *** ", 'grey'))
                    rounds = 0
                    RPS.restart()
                else:
                    continue

            elif (player is 1 and comp is 'Scissors') or (player is 2 and comp is 'Rock') \
                or (player is 3 and comp is 'Paper'):

                print("Player Choose: {}".format(moves[player - 1]))
                print("Computer Choose: {}".format(comp))
                print(colored("*** Round #: {} | Result: Player Wins !! *** ", 'blue').format(rounds))

                if rounds >= r:
                    print(colored("*** GAME OVER *** ", 'grey'))
                    rounds = 0
                    RPS.restart()
                else:
                    continue
            else:
                print(colored("No valid input!", 'red'))
                RPS.game()

if __name__ == '__main__':
    #rounds = ""
    while True:
        RPS.show_welcome()
        rundnum = RPS.round_number()
        RPS.game()
        rounds = 0
        rundnum = 0

问题来源StackOverflow 地址:/questions/59383812/round-counter-wont-reset-when-restarting-the-game-when-user-input-is-invalid

展开
收起
kun坤 2019-12-26 15:50:52 370 0
1 条回答
写回答
取消 提交回答
  • 当您在函数本身中调用rpm .game()时,问题就出现了。这将创建一种情况,即所有变量都将按照.game()函数的定义重置。为了避免这种情况,可以为函数提供默认参数,然后使用这些参数调用函数(在函数内部)。 示例(使用部分原代码):

    def game(player=0, rounds=0):
    
        # player = 0
        # rounds = 0
        moves = ['Rock', 'Paper', 'Scissors',]
        r = rundnum
    
        while rounds < r:
            rounds += 1
            comp = random.choice(moves)
    
            print(colored("Choose : ", 'green'), colored("1)Rock", 'red'), colored("2)Paper", 'yellow'),
                  colored("3)Scissors", 'grey'))
    
            try:
                player = int(input("What's Your Guess? "))
            except:
                print(colored("No Valid Input! Restarting Game...", 'red'))
                RPS.game(player, rounds) # Here's the magic...
    
    2019-12-26 15:50:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载