这是我的第一个问题,所以我希望我能说得对。
我在Python中做了一些预定义练习,一个请求是定义以下公式:
用户在键盘输入时,{6 * [2+(a-1)%b] * 2a} 。
如果我要选择" a"和" b"我的自己,公式正在运行,但是,当询问用户的输入(raw_input
)时,我收到错误,我无法弄清楚原因。
我试过的代码看起来像这样:
a = raw_input("enter the first number")
b = raw_input("enter the second number")
print (6*(2+(a-1)%b)*2**a)
错误信息是:
Traceback (most recent call last):
File "python", line 4, in <module>
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'
提前感谢您的帮助, 维克多
答案 0 :(得分:1)
在你不幸选择使用的python2中,函数raw_input
返回一个字符串。您必须将其转换为数字:
a = float(raw_input("enter the first number"))
b = float(raw_input("enter the second number"))
并且2**a
可能应该是2*a
,除非你将2加权a
。