-2

有几个问题有同样的错误,但是我刚开始Python,看不懂。我正在尝试创建一个程序,它生成两个随机数,给这些数字起昵称,询问用户答案是什么,将这两个数字相乘并给那个数字起一个昵称,然后它读取用户的答案并查看用户的答案和它得到的一样。我从一个更简单的程序开始,它会问同样的问题 3 次。

我的程序如下:

def math ():
    for f in range (3):
        x=10
        c=5
        x*c=p
        print x,'times',c,'.'
        v=read_number('What is the answer?')
        if p==v:
            print 'You got it right!'
        else:
            print 'You got it wrong.'

现在我在第 6 行查看它是不必要的。但是当我完成它时,Python 说“语法错误:无法分配给运算符。” 它还在 c=5 之后突出显示。为什么会发生这种情况,我该如何解决?

4

2 回答 2

4

x*c=p

应该读

p=x*c

这乘以x并将c结果分配给p

于 2014-02-17T18:37:22.410 回答
0

您使用 random 模块中的 randint() 生成随机数,例如:

from random import randint

x = randint(0,10)   # random between 0 and 10
c = randint(0,10)

p = x * c

print p
于 2014-02-17T18:58:49.247 回答