python 忽略 if 语句[重复]

问题描述 投票:0回答:3

我一直在开发一个Python程序,用户可以在其中与计算机玩石头、剪刀、布。

系统会提示用户输入 0 表示布、1 表示石头、2 表示剪刀。我的程序会打印每个玩家选择的内容,理想情况下我希望它也能说“纸覆盖岩石。你赢了”之类的内容,但我似乎无法让我的 if 语句起作用,而且我希望我的程序打印“你选择了”布”等,但它仍然显示 0,1 和 2,而不是石头、布或剪刀。

我尝试分配 paper = 0。我想它可能不起作用,因为数字不是字符串,所以我尝试添加括号 paper =“0”,但这也不起作用。另外,当我创建变量“player”时,我认为我正确地使用了字符串函数将输入的任何整数转换为字符串,但也许有些东西我没有看到。

后来我尝试避免将布、石头和剪刀分配给整数,而是创建新变量。然而,在我的 if 语句中创建的新变量被声明为未定义,所以我一定以某种方式搞砸了。我很困惑为什么它不起作用,因为其他人出于相同目的使用 if 语句创建新变量编写了一个程序,而他的程序起作用了。

我认为可能存在错误的另一个地方是我如何用 elif 而不是 else 结束 if 语句,所以我改变了它并得到了另一个不正确的语法错误。我一直在阅读我的教科书并在互联网上搜索,所以如果我错过了一些应该显而易见的东西,我很抱歉。我对编程非常非常非常陌生,所以我的知识非常有限。这段代码今天已经变形了很多,我什至不记得它至少能够完成编译而不会遇到错误时是怎样的。

现在这段代码会遇到错误,因为player1 没有定义。很抱歉这个程序太让人眼花缭乱了。

    import random
    player = input(str("Enter 0 for paper, 1 for rock, and 2 for scissors:" ))
    computer = str(random.randint(0,2))
    if player == 0:
       player1 == "paper"
    elif player == 1:
       player1 == "rock"
    elif player == 2:
       player1 == "scissors"
    elif computer == 0:
       computer1 == "paper"
    elif computer == 1:
       computer1 == "rock"
    elif computer == 2:
       computer1 == "scissors"
    print ("You chose " + player1 , "and the computer chose " , + computer1)
    if player == "paper" and computer == "rock":
       print("Paper covers rock. You win!")
    elif player == "paper" and computer == "scissors":
       print("Scissors cut paper. You lose!")
    elif player == "paper" and computer == "paper":
       print("You both chose paper. It's a draw!")
    elif player == "rock" and computer == "paper":
       print("Paper covers rock. You lose!")
    elif player == "rock" and computer == "rock":
       print("You both chose rock. It's a draw!")
    elif player == "rock" and computer == "scissors":
       print("Rock beats scissors. You win!")
    elif player == "scissors" and computer == "paper":
       print("Scissors cut paper. You win!")
    elif player == "scissors" and computer == "rock":
       print("Rock beats scissors. You lose!")
    elif player == "scissors" and computer == "scissors":
       print("You both chose scissors. It's a draw")

更新:在一些错误引起我的注意后,我修复了一些问题,现在我的代码如下所示:

    import random

    player = input("Enter 0 for paper, 1 for rock, and 2 for scissors:" )
    computer = random.randint(0,2)

    if player == 0:
       player1 = "paper"
    elif player == 1:
       player1 = "rock"
    elif player == 2:
       player1 = "scissors"

    if computer == 0:
       computer1 = "paper"
    elif computer == 1:
       computer1 = "rock"
    elif computer == 2:
       computer1 = "scissors"

    print ("You chose " + player1 , "and the computer chose " + computer1)

    if player1 == "paper" and computer1 == "rock":
       print("Paper covers rock. You win!")
    elif player1 == "paper" and computer1 == "scissors":
       print("Scissors cut paper. You lose!")
    elif player1 == "paper" and computer1 == "paper":
       print("You both chose paper. It's a draw!")
    elif player1 == "rock" and computer1 == "paper":
       print("Paper covers rock. You lose!")
    elif player1 == "rock" and computer1 == "rock":
       print("You both chose rock. It's a draw!")
    elif player1 == "rock" and computer1 == "scissors":
       print("Rock beats scissors. You win!")
    elif player1 == "scissors" and computer1 == "paper":
       print("Scissors cut paper. You win!")
    elif player1 == "scissors" and computer1 == "rock":
       print("Rock beats scissors. You lose!")
    elif player1 == "scissors" and computer1 == "scissors":
       print("You both chose scissors. It's a draw")

我现在收到错误:

   NameError: name 'player1' is not defined
python if-statement python-3.x syntax-error
3个回答
5
投票

使用

==
进行比较,使用
=
进行作业:

if player == 0:
   player1 = "paper"
....

2
投票

在您的更新版本中:

player = input("Enter 0 for paper, 1 for rock, and 2 for scissors:" )
computer = random.randint(0,2)
if player == "0":
   player1 = "paper"
elif player == "1":
  player1 = "rock"
elif player == "2":
  player1 = "scissors"
elif computer == "0":
   computer1 = "paper"
elif computer == "1":
   computer1 = "rock"
elif computer == "2":
   computer1 = "scissors"

(至少)有两个问题导致

computer1
永远无法被定义。

首先,

elif
的意思是“else if”——换句话说,如果
player
等于
"0"
"1"
"2"
中的任何一个,那么这些
computer
测试都不会发生。

其次,您将

computer
定义为整数 -
0
1
2
。数字不可能等于字符串,因此所有比较都将是错误的。

要解决这两个问题(这可能不是代码中的所有问题,只是导致此问题的两个问题

NameError
),您需要这个:

if player == "0":
   player1 = "paper"
elif player == "1":
  player1 = "rock"
elif player == "2":
  player1 = "scissors"
else:
  print "player is", player, "rather than a string for 0, 1, or 2!"

if computer == 0: # NOTE: not elif, and not "0"
   computer1 = "paper"
elif computer == 1:
   computer1 = "rock"
elif computer == 2:
   computer1 = "scissors"
else:
   print "computer is", computer, "instead of 0, 1, or 2!"

但是,更好的方法是使用一致的类型而不是随机混合数字和字符串,并使用列表或字典而不是一长串

if
语句。例如:

prs = ["paper", "rock", "scissors"]
player = int(input("Enter 0 for paper, 1 for rock, and 2 for scissors:"))
computer = random.randint(0, 2)
player1 = prs[player]
computer1 = prs[computer]

现在,除了避免大量重复之外,您还可以确保尽快出现任何问题 - 如果用户键入

spam
76
,您将收到一个异常,告诉您
'spam'
可以不要立即将其转换为数字,或者
76
不是有效索引,而不是在 20 行后获得关于
NameError
player1
computer1


2
投票

正如我在评论中提到的以及其他答案中指出的,您至少有两个问题:

  1. 您使用的是
    ==
    相等运算符,而不是引入赋值语句的
    =
    ,因此您根本没有设置
    player1
    computer1
    变量的值。
  2. 您没有任何特殊原因将整数转换为字符串,然后将这些字符串与整数进行比较。
    str(x) == int(x)
    永远
    False
    ;与 Perl 或 PHP 不同,Python 不会将字符串隐式转换为数字以与数字类型进行比较。

您的代码可以大大简化。不需要为每组可能性都有一个单独的

if
声明。您可以使用
list
dict
来存储投掷的名称(“布”、“石头”、“剪刀”)与其数值之间的对应关系,然后利用游戏的自然对称性。这是我的演绎:

import random

throws = ["paper","rock","scissors"]

player = None
while player not in throws:
    player = input("Enter your throw (%s): " % ', '.join(throws))
player = throws.index(player)

computer = random.randint(0,2)

print ("You chose %s and the computer chose %s" % (throws[player], throws[computer]))
outcome = (player-computer)%3
if outcome==0:
    print("You both chose %s. It's a draw!" % throws[player])
elif outcome==1:
    print("%s beats %s. You lose!" % (throws[computer].title(), throws[player]))
elif outcome==2:
    print("%s beats %s. You win!" % (throws[player].title(), throws[computer]))
© www.soinside.com 2019 - 2024. All rights reserved.