python忽略if语句

时间:2014-09-26 20:49:58

标签: python if-statement python-3.x syntax-error

我一直在研究python程序,用户在计算机上玩摇滚,纸张和剪刀。

提示用户输入0表示纸张,1表示摇滚,2表示剪刀。我的程序将打印每个玩家选择的内容,理想情况下我希望它也能说“Paper cover rock。你赢了”但我似乎无法让我的if语句工作,而且我希望我的程序打印“你选择了纸张“等等,但它代替摇滚,纸张或剪刀,它仍然说0,1和2。

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

后来我试图避免将纸张,摇滚和剪刀分配给整数,而是创建新的变量。但是,在我的if语句中创建的新变量被声明为undefined,所以我必须以某种方式搞砸了它。我很困惑为什么它不起作用,因为其他人为了相同的目的编写了一个程序,使用if语句创建新变量并且他的程序工作。

我认为可能存在错误的另一个地方是我如何用elif而不是其他方式结束我的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

3 个答案:

答案 0 :(得分:5)

使用==进行比较,使用=进行分配:

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

答案 1 :(得分:2)

正如我在评论中提到的并且在其他答案中指出的那样,你至少有两个问题:

  1. 您正在使用==等于运算符而不是引入赋值语句的=,因此您不会设置player1或{{的值1}}变量。
  2. 您无需特殊原因将整数转换为字符串,然后将这些字符串与整数进行比较。 computer1 始终str(x) == int(x);与Perl或PHP不同,Python不会隐式地将字符串转换为数字,以便与数字类型进行比较。
  3. 您的代码可以大大简化。对于每组可能性,不需要单独的False语句。您可以使用iflist来存储投掷名称之间的对应关系("纸张","摇滚","剪刀&# 34;)和他们的数值,然后利用游戏的自然对称性。以下是我的演绎:

    dict

答案 2 :(得分: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定义为整数 - 012。数字不能等于字符串,因此所有比较都将是错误的。

要解决这两个问题 - 这可能不是代码中的所有问题,只是导致这个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]

现在,除了避免大量重复之外,您还要确保尽快显示任何问题 - 如果用户输入spam76,您将收到异常消息'spam'无法立即转换为数字,或76不是有效索引,而不是NameError player1或{{computer1 1}} 20行之后。