我在Python的剪刀游戏中遇到了一个问题(在返回部分)

e3bfsja2  于 2022-10-22  发布在  Python
关注(0)|答案(1)|浏览(154)

嗯,我用python编写了我的剪刀代码,一切都很好,直到我努力使它变得更复杂。直到第28行,一切都很好,就像一个普通的剪刀游戏,但在那之后,我想说的是,对于“你想继续还是不想继续”这个问题,我们只有两个正确的答案,只要答案是肯定的,就返回整个游戏并重复它(顺便说一句,因为返回代码,我添加了一个def并将其称为RPS),如果用户的答案是否定的,就打断它并说再见,但当我运行代码时,我在终端中收到的唯一消息是“点击任何东西继续”。如果你知道答案,请帮助我
这是我的代码:

import random
def RPS():
 while True:
  user_action = input("Please enter rock, paper or scissor : ")
  possible_actions = ["rock" , "paper" , "scissor"]
  computer_action = random.choice(possible_actions)

  if user_action == computer_action:
     print(f"Your choice is same as your computer . . . It's a tie!!!")

  elif user_action == "rock":
     if computer_action == "scissor":
        print(f"You Won!!! your choice was rock and the computer choice was scissor")
     else:
        print("Paper covers rock! You lose.")

  elif user_action == "paper":
     if computer_action == "rock":
        print(f"You won!!! your choice was rock and the computer choice was rock")
     else:
        print("Scissors cuts paper! You lose.")

  elif user_action == "scissor":
     if computer_action == "paper":
        print(f"You won!!! your choice was scissor and the computer choice was paper")
     else:
        print("Rock smashes scissor. You lose!!!")
  play_again = input("Play again? (y/n): ")

  possible_responses = {'y' , 'n'}
  while possible_responses == play_again.lower():
   if play_again.lower()=='y':
      return(RPS)
   else:
    break
  print(f"Alright. good bye")
eoxn13cs

eoxn13cs1#

我修正了代码的缩进,并以正确的方式结束了循环。我认为问题在于,在第二个循环中使用了break命令,而没有结束第一个循环,该循环一直处于“True”状态。我通过重写第二个循环来解决这个问题,使连续输入无限循环,直到它识别出possible_responses集合中的答案。一旦这样,它就会打破第二个循环。
接下来,它检查play_again是否不是“y”。如果是,则结束整个函数。否则,它会再次循环。希望这有帮助!:D

import random

def RPS():
    while True:
        user_action = input("Please enter rock, paper or scissor : ")
        possible_actions = ["rock" , "paper" , "scissor"]
        computer_action = random.choice(possible_actions)

        if user_action == computer_action:
            print(f"Your choice is same as your computer . . . It's a tie!!!")

        elif user_action == "rock":
            if computer_action == "scissor":
                print(f"You Won!!! your choice was rock and the computer choice was scissors")
            else:
                print("Paper covers rock! You lose.")

        elif user_action == "paper":
            if computer_action == "rock":
                print(f"You won!!! your choice was rock and the computer choice was rock")
            else:
                print("Scissors cuts paper! You lose.")

        elif user_action == "scissor":
            if computer_action == "paper":
                print(f"You won!!! your choice was scissor and the computer choice was paper")
            else:
                print("Rock smashes scissor. You lose!!!")

        possible_responses = {'y', 'n'}
        while True:
            play_again = input("Play again? (y/n): ")
            if play_again.lower() in possible_responses:
                break
            else:
                print('Unrecognized response. Please try again.')
        if play_again.lower()!='y':
            print(f"Alright. good bye")
            return False

相关问题