Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在这段代码中循环使用石头、布、剪刀?_Python_Python 3.x - Fatal编程技术网

如何使用python在这段代码中循环使用石头、布、剪刀?

如何使用python在这段代码中循环使用石头、布、剪刀?,python,python-3.x,Python,Python 3.x,这是代码 import random Rock = '1' Paper = '2' Scissors = '3' print('Welcome to Rock, Paper Scissors! The game of all kids to decide on something. \nIn this game you will have to beat the computer once. \n(Psst if it\'s a draw the start the program again!

这是代码

import random
Rock = '1'
Paper = '2'
Scissors = '3'
print('Welcome to Rock, Paper Scissors! The game of all kids to decide on something. \nIn this game you will have to beat the computer once. \n(Psst if it\'s a draw the start the program again! ;D)\nSo how to play. Well, it\'s simple. Pick 1 for Rock, 2 for Paper and 3 for Scissors. \nSo Rock beats Scissors. Scissors cuts Paper and Paper covers Rock. Got it Lets play')
player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
if player<1 or player>3 except player==9:
   player=int(input('Invalid number. Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
   if player<1 or player>3:
       print('Well, now you can\'t play this game because you are mucking around. Next time DON\'T!')
elif player==9:
    exit()
else:
   computer=random.randint(1, 3)
   print(player,computer)
   print('Remember Rock = 1, Paper = 2 and Scissors = 3')
   if player==1 and computer==1 or player==2 and computer==2 or player==3 and computer==3:
       print('It\'s a draw. =l Restart the game if you want to.')
   if player==1 and computer==2 or player==2 and computer==3 or player==3 and computer==1:
       print('Computer wins! You lose. Sorry. =(')
   if player==1 and computer==3 or player==2 and computer==1 or player==3 and computer==2:
       print('You have won. Well done. =D')
随机导入
摇滚乐='1'
纸张='2'
剪刀='3'
print('欢迎来到石头,剪刀!这是所有孩子决定某事的游戏。\n在这个游戏中,你必须打败电脑一次。\n(如果是平局,请再次启动程序!;D)\nSo如何玩。很简单。选择1块石头,2块布,3块剪刀。\nSo石头胜过剪刀。剪刀剪纸,布盖住石头。明白了,让我们玩吧!)
player=int(输入('请输入数字1=石头,2=布,3=剪刀:'))
如果播放机3(播放机除外)=9:
player=int(输入('无效数字。请输入数字1=石头,2=布,3=剪刀:'))
如果播放者3:
print(‘好吧,现在你不能玩这个游戏了,因为你在胡闹。下次别玩了!’
elif播放器==9:
退出()
其他:
计算机=random.randint(1,3)
打印(播放器、计算机)
打印(‘记住石头=1,纸=2,剪刀=3’)
如果player==1,computer==1,或者player==2,computer==2,或者player==3,computer==3:
print('这是一场平局。=如果你愿意,我会重新开始比赛。')
如果player==1,computer==2,或者player==2,computer==3,或者player==3,computer==1:
打印('计算机赢了!你输了。对不起。=(')
如果player==1,computer==3,或者player==2,computer==1,或者player==3,computer==2:
打印('您赢了。做得好。=D')

我需要循环它,这样当用户输入错误的号码时,它会再次被询问,直到用户正确回答。我还需要循环播放程序,直到用户按9退出。

使用循环,您可以控制游戏何时结束

game_over = False
while not game_over:
    do_stuff()
    if some_condition:
        game_over = True

在循环结束时,它会检查游戏是否结束。如果结束了,循环将退出,循环运行后会出现任何代码。然后程序就存在了。

要保持循环,您可以做的最小更改就是添加一个
while True
语句,并将游戏的其余部分嵌套在它下面。我还将为退出c设置测试就像这样

...
print('Welcome to Rock, Paper Scissors! ...')
while True:  # loop forever
  player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
  if player==9:  # but exit if 9
      exit()
  elif player<1 or player>3:
     ...
  else:
     ...
。。。
印刷(‘欢迎来到石头、剪纸!……’)
虽然是真的:#永远循环
player=int(输入('请输入数字1=石头,2=布,3=剪刀:'))
如果玩家==9:#但如果是9,则退出
退出()
elif player3:
...
其他:
...

它应该像这样的循环一样简单

while True:
  while player not in (1,2,3,9):
    keep_asking_for_input()
  if player == 9:
    break
  pass

重写你的代码,使用def和类。这会容易得多。你需要在循环时修改你的代码

我从零开始就为你写了完整的游戏。 在这里:


阅读代码并理解它是如何工作的。请随意使用它。顺便说一下,它在Python2.x中(很抱歉,我没有使用Python3).

如果你知道循环是什么,我在这里向你介绍python的
while/for
;查找其中任何一个,尝试编写循环,然后就出现的错误寻求帮助。你看了吗?
end
不是有效的关键字,是吗?不是。对不起,python/Ruby在这里有一点混乱。