摇滚纸剪刀游戏,无限循环

时间:2018-04-22 06:37:05

标签: 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(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!

3 个答案:

答案 0 :(得分:1)

这一行

http://localhost:4000
如果没有平局,

将始终为if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'): 。将其更改为

True

改善代码的一些建议

您可以删除所有if player not in options: 中的()。此

if

相同
if (player == npc):

您还应该使用if player == npc: 而不是if/elif/else。这样就可以使用if unncessary。

修改:改进版本:

continue

答案 1 :(得分:1)

您的计划中存在逻辑错误。

特别是这一行:

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

如果至少有一个它所链接的语句为True,则'或'运算符返回True。

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

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

您可以使用'和'代替'或'来轻松解决此问题:

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

此处,语句变为False and True and True,这是假的。仅当玩家按预期输入的选项不是Rock, Paper, Scissor选项时,此语句才返回True。

更多Pythonic的做法是用以下内容替换整个语句,如另一个答案所述:

if player not in options:

答案 2 :(得分:0)

我建议您进行一些改进,例如使用if-elif,而不是continue。同时使用.format(...)表示回复。

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

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

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