【问题标题】:Trying to do input validation for rps game尝试对rps游戏进行输入验证
【发布时间】:2018-09-29 07:17:44
【问题描述】:

我正在 python 3.6 上制作 rps 游戏,但似乎无法弄清楚如何为我的两人游戏功能模块进行输入验证。每当我运行程序并选择两人游戏并输入小于 1 或大于 4 的数字时,它只会询问我是否想再玩一次。

   def twoPlayerGame():
        player1 = False
        player2 = 0
        player1Win = 0
        player2Win = 0
        tie_ = 0

        #prompting for weapon choice
        while player1 == False:
            weaponMenu()
            player1 = int(input("Player 1, please make a selection.\t"))
            player2 = int(input("Player 2, please make a selection.\t"))

            while player1 or player2 not in range(1,4):
                print ("Invalid data entered, try again.")
                weaponMenu()
                player1 = int(input("Player 1, please make a selection.\t"))
                player2 = int(input("Player 2, please make a selection.\t"))

            #redirects to main
            while player1 == 4 or player2 == 4:
                main()

            if player1 == player2:
                print("It's a tie!\n")
                tie_ = tie_ + 1
            elif player1 == 1:
                if player2 == 2:
                    print("Paper covers rock! Player 2 won!\n")
                    player2Win = player2Win + 1
                else:
                    print("Rock smashes scissors! Player 1 won!\n")
                    player1Win = player1Win + 1
            elif player1 == 2:
                if player2 == 3:
                    print("Scissors cuts paper! Player 2 won!\n")
                    player2Win = player2Win + 1
                else:
                    print("Paper covers rock! Player 1 won!\n")
                    player1Win = player1Win + 1
            elif player1 == 3:
                if player2 == 1:
                    print("Rock smashes scissors! Player 2 won!\n")
                    player2Win = player2Win + 1
                else:
                    print("Scissors cuts paper! Player 2 won!\n")
                    p1ayer2Win = player2Win + 1


            #ask user if they want to play again
            again = input("Play again? Enter yes or no.\n")
            again = again.lower()
            #display scores
            print("Ties:\t", tie_)
            print("Player 1 wins:\t", player1Win)
            print("Player 2 wins:\t", player2Win)

            #if yes, player still playing
            #if no, redirect to main
            if again=="yes":
                player1 = False
            else:
                player1 = True
                main()

【问题讨论】:

    标签: python-3.x input


    【解决方案1】:

    问题出在这一行:

     while player1 or player2 not in range(1,4):
    

    无论您在or 的每一侧放置什么,通常都应该是真值或假值。将player1 放在左侧,虽然 Python 解释器可以接受,但在这里是不正确的,因为您要做的是检查它是否在范围内。在右侧,player2 not in range(1,4) 是一个实际的真/假值,所以没关系。

    所以把左边的东西改成player1 not in range(1,4),函数就可以正常工作了:

    while player1 not in range(1,4) or player2 not in range(1,4):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2020-08-24
      • 1970-01-01
      • 2019-10-30
      • 2021-07-18
      相关资源
      最近更新 更多