首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石头剪刀游戏,无限循环

石头剪刀游戏,无限循环
EN

Stack Overflow用户
提问于 2018-04-22 06:37:05
回答 3查看 559关注 0票数 2
代码语言:javascript
复制
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

它不停地打印,这是一个领带,不会显示任何其他结果。我刚开始编程。任何输入都将不胜感激!

编辑:循环已经解决了。

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

代码语言:javascript
复制
   Output: Hello
           We are about to play Rock,Paper,Scissors.
           Please declare your weapon: rock
           Your choice:  Rock
           npc choice:  Paper
           You lose!
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-04-22 06:43:04

这条线

代码语言:javascript
复制
if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):

如果没有领带的话,永远都是True。把它改成

代码语言:javascript
复制
if player not in options:

一些改进代码的建议

您可以删除所有()s中的if

代码语言:javascript
复制
if (player == npc):

是相同的

代码语言:javascript
复制
if player == npc:

您还应该使用if/elif/else,而不是只使用if。这将使continue的使用成为必然。

编辑:改进版本:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2018-04-22 07:00:43

程序中有一个逻辑错误。

具体而言,这一行:

代码语言:javascript
复制
if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):

如果它链接的语句中至少有一个是True,则'or‘运算符返回True。

例如,假设玩家选择了“摇滚”。现在,第一个语句player != 'Rock'是假的,但是第二个player != 'Paper'是True,player != 'Scissor'也是。

因此,整个语句变成了False or True or True,它是True,程序最后告诉用户他们的选择无效。

您可以通过使用'and‘而不是’或‘之类的方法来轻松地修复这个问题:

代码语言:javascript
复制
if (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):

在这里,语句变成False and True and True,这是假的。只有当玩家输入了一个不是选项之一的选择时,此语句才会返回True:Rock, Paper, Scissor,正如预期的那样。

要做到这一点,一个更重要的方法是将整个声明替换为以下内容,如另一个答复中所提到的那样:

代码语言:javascript
复制
if player not in options:
票数 1
EN

Stack Overflow用户

发布于 2018-04-22 07:25:38

我建议您做一些改进,比如使用if-elif,而不是continue。还可以使用.format(...)来表示答复。

对于循环问题,将第二个if语句中的逻辑运算符更改为包含迭代的and操作符。

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49963241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档