【问题标题】:Python 3.4: How do I assign variables to operators?Python 3.4:如何将变量分配给运算符?
【发布时间】:2014-12-06 21:25:51
【问题描述】:

我正在尝试创建一个快速程序,它将对多项式进行 4 次合成除法,但是当我尝试执行我的代码时,它会告诉我 R*A:无法分配给运算符。 我认为这意味着它不能进行乘法运算,但为什么呢?我的编程经验有限,Java CompSci 才一年

print("This program assumes that the polynomial is to the 4th degree")
A = input('Input the first coefficient: ')
B = input('Input the second coefficient: ')
C = input('Input the third coefficient: ')
D = input('Input the fourth coefficient: ')
E = input('Input constant: ')
R = input('Input the divisor: ')
temp = 0

R*A = temp
#B + temp = temp
#R * temp = temp
#C + temp = temp
#R * temp = temp
#D + temp = temp
#R * temp = temp
#E + temp = temp

if temp == 0:
    print("It works!")
else:
        print("dang")

input('This is a shitty workaround for pause')

【问题讨论】:

  • 不要尝试分配给乘法,而是分配给temptemp = R * A。换句话说,听起来好像你的语法顺序搞混了。
  • else: 之后的缩进也是双倍的!

标签: python division synthetic


【解决方案1】:

我假设您没有尝试重新分配运算符,而只是进行乘法运算。

在 python 中,与包括 Java 在内的许多其他语言一样,赋值是这样完成的:

temp = R*A

【讨论】:

  • @OberstRyan Python 3 不会自动更改类型。使用int(input(...)) 转换为整数。
【解决方案2】:

温度 = R * A

您的输入将是 str。你需要告诉python它将是一个int。 int (input("...."))

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 2020-01-18
    • 2017-01-11
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多