Python语法错误行17

时间:2014-10-26 05:38:10

标签: python

我一直在为我的班级编写一段简单的代码,我已经尝试过几次,甚至我的老师都不确定为什么它不起作用。我目前从第17行开始出现语法错误。欢迎任何帮助。 :)

00000001 import random
00000002 
00000003 def main():
00000004     print("Intro message.")
00000005 
00000006     play_again = "Y"
00000007     
00000008     while play_again == "Y" or play_again == "y":
00000009         play_game()
00000010     
00000011     play_again = input("Do you want to play again:")
00000012     
00000013     
00000014 def play_game():
00000015     computer_choice = get_computer_choice()
00000016     player_choice = get_player_choice()
00000017     if computer_choice = 1:
00000018         print("Computer has chosen rock.")
00000019     elif computer_choice = 2:
00000020         print("Computer has chosen paper.")
00000021     else computer_choice = 3:
00000022         print("Computer has chosen scissors.")
00000023         winner = determine_the_winner(computer_choice,player_choice)
00000024         return winner
00000025         
00000026 def computer_choice():
00000027     choice = random.randint(1,3)
00000028     return choice
00000029     
00000030 def get_player_choice():
00000031     choice = input("enter 1,2, or 3")
00000032     while choice not = 1,2,3
00000033     print("error message")
00000034     choice = input("enter 1,2, or 3")
00000035     return choice    
00000036     
00000037 def determine_the_winner(computer_choice, player_choice):
00000038     if computer_choice==player_choice:
00000039         print("Result is a tie.")
00000040     elif computer_choice == 1 and player_choice == 3 or computer_choice ==2 and player_choice =1 or computer_choice ==3 and player_choice ==2:
00000041         print("You win.")
00000042             
00000043 
00000044 main()

2 个答案:

答案 0 :(得分:2)

您需要使用==进行比较而不是=

=用于创建变量,正如您所使用的那样。

例如,if computer_choice = 1应为if computer_choice == 1

答案 1 :(得分:2)

你有一些问题。正如Haidro所说,你正在使用=(赋值),你应该使用==(相等检查),但你还有其他问题。

让我们从这开始:

while choice not = 1,2,3

这是在get_player_choice()方法中。这行不是有效的Python语法,你可能想要:

while choice not in ['1','2','3']:

(你也应该正确地缩进循环)。

在此行play_game的{​​{1}}方法中,您正在调用方法computer_choice = get_computer_choice(),其中您的方法实际上称为get_computer_choice

在同一方法中进一步向下调用computer_choice(),但在定义player_choice = get_player_choice()后,此方法已定义为

此外,如果计算机选择3,您只检查获胜者并返回结果。除了else条件之外的return语句之外,您还需要移动此行play_game - 换句话说,您需要减少线的缩进。