2016-10-04 102 views
-3

我正在尝试做一个简单的猜谜游戏作为一个学校作业。我开始使用一个长版本,并试图通过使用循环使其更短,但我得到了一个无法分配给第13行的操作员错误,这在我尝试执行A1 + 1 = A1时不明白。不能分配给操作员错误

import sys 
import random 

A1 = 1 
A2 = 2 
A3 = 1 
Array=[] 
Len = int(input("How long do you what the game to be")) 
for x in range(11): 
G = random.randint(1,13) 
Array.append(G) 
for x in range(Len): 
    A1 + 1 = A1 
    A2 + 1 = A2 
    A3 + 1 = A3 
print(Array[A3]) 
Num = Array[A3] 
G1 = input("Is the next number higher or lower.") 
if Array[A1] - Array[A2] > 1: 
    print("Correct") 
    print("_______________________________________________________________________________") 
else: 
    print("YOu FaIl") 
    print("_______________________________________________________________________________") 
    sys.exit() 
print("Well done you have won the game CONGRATS!") 
+2

我认为你的意思'A1 + = 1'? –

+1

'A1 + 1 = A1'你的理解应该在这里发生什么? –

+2

您必须了解赋值运算符。左侧是受让人,右侧是分配给受让人的价值。另外,如果你从逻辑上看代码,你怎样才能给一个不同的值赋一个常量(一个数字)? – Li357

回答

3

作业去<variable> = <expression>,而不是相反。

这可更换

for x in range(Len): 
    A1 + 1 = A1 
    A2 + 1 = A2 
    A3 + 1 = A3 

通过这个(假设你想添加一个到每个这些变量的Len倍)

A1 += Len 
A2 += Len 
A3 += Len 
相关问题