Python 石头剪刀布游戏,无限循环

Python 石头剪刀布游戏,无限循环,python,Python,它一直打印出来,这是一个平局,不会显示任何其他结果。我刚开始编程。任何意见都将不胜感激 编辑:循环已解决 这是根据请求的示例输出: import random options = ['Rock','Paper', 'Scissor'] npc = random.choice(options) print('Hello') print('We are about to play Rock,Paper,Scissors.') while True: npc = random.choice(

它一直打印出来,这是一个平局,不会显示任何其他结果。我刚开始编程。任何意见都将不胜感激

编辑:循环已解决

这是根据请求的示例输出:

import random

options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)

print('Hello')
print('We are about to play Rock,Paper,Scissors.')

while True:
  npc = random.choice(options)
  player = str(input('Please declare your weapon: ')).capitalize()
  if (player == npc):
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('Oopsie looks like we have a tie!')
    print('Lets Try again!')
    continue
  if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):
    print('Poo Poo, that is not a valid option! Please try again!')
    continue
  if ((player == 'Rock') and (npc == 'Scissor')) or ((player == 'Scissor') and (npc == 'Paper')) or ((player == 'Paper') and (npc == 'Rock')):
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('You win!')
    break
  if ((player == 'Rock') and (npc == 'Paper')) or ((player == 'Scissor') and (npc == 'Rock')) or ((player == 'Paper') and (npc == 'Scissor')):
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('You lose!')
    break
这条线

   Output: Hello
           We are about to play Rock,Paper,Scissors.
           Please declare your weapon: rock
           Your choice:  Rock
           npc choice:  Paper
           You lose!
如果没有平局,则始终为
True
。换成

if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):
一些改进代码的建议

如果s,您可以删除所有
中的
()
s。这个

if player not in options:

if (player == npc):
您还应该使用
if/elif/else
,而不仅仅是
if
。这将不需要使用
continue

编辑:改进版:

if player == npc:

您的程序中有一个逻辑错误

具体而言,这一行:

import random

options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)

print('Hello')
print('We are about to play Rock,Paper,Scissors.')

while True:
  npc = random.choice(options)
  player = str(input('Please declare your weapon: ')).capitalize()
  if player == npc:
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('Oopsie looks like we have a tie!')
    print('Lets Try again!')
  elif player not in options:
    print('Poo Poo, that is not a valid option! Please try again!')
  elif (player == 'Rock' and npc == 'Scissor') or (player == 'Scissor' and npc == 'Paper') or (player == 'Paper' and npc == 'Rock'):
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('You win!')
    break
  else:
    print('Your choice: ', player)
    print('npc choice: ', npc)
    print('You lose!')
    break
如果“or”运算符链接的语句中至少有一条为True,则该运算符返回True

例如,假设玩家选择了“摇滚乐”。现在,第一条语句
player!='摇滚乐“
为假,但第二个是
玩家!='文件“
是正确的,
player!=”也是正确的剪刀“

因此,整个语句变为
False或True或True
,这是True,程序最终会告诉用户他们的选择无效

您可以使用“and”而不是“like so”轻松解决此问题:

if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):
在这里,语句变为
False和True,而True则变为False。仅当玩家输入的选项不是所需的选项之一时,此语句才会返回True:
岩石、纸张、剪刀

另一个答案中提到的,一个更具python风格的方法是用以下语句替换整个语句:

if (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):

我建议您进行一些改进,例如如果elif
,则使用
,而不是
continue
。还可以使用
.format(…)
表示答复

对于循环问题,将第二个if语句中的逻辑运算符更改为
运算符以进行包含式迭代

最终格式化的代码如下所示:

if player not in options:

@我明白了,卢茨的解决方案解决了循环问题!您还可以发布脚本的输出吗?@dstrants我已经发布了。@dstrants输出将取决于
random.choice的输入和结果。只需阅读代码就可以很容易地发现代码中的错误。看看我编辑的答案。我希望这对你有帮助。有多个if语句是一种不好的做法吗?还是elif语句在某种程度上更好?
import random

options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)

print('Hello')
print('We are about to play Rock,Paper,Scissors.')

while True:
    npc = random.choice(options)
    player = str(input('Please declare your weapon: ')).capitalize()
    if (player == npc):
        print('Your choice: {}'.format(player))
        print('npc choice: {}'.format(npc))
        print('Oopsie looks like we have a tie!')
        print('Lets Try again!')
    elif (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):
        print('Poo Poo, that is not a valid option! Please try again!')
    elif ((player == 'Rock') and (npc == 'Scissor')) or ((player == 'Scissor') and (npc == 'Paper')) or ((player == 'Paper') and (npc == 'Rock')):
        print('Your choice: {}'.format(player))
        print('npc choice: {}'.format(npc))
        print('You win!')
        break
    elif ((player == 'Rock') and (npc == 'Paper')) or ((player == 'Scissor') and (npc == 'Rock')) or ((player == 'Paper') and (npc == 'Scissor')):
        print('Your choice: {}'.format(player))
        print('npc choice: {}'.format(npc))
        print('You lose!')
        break