我试图用python编写一个程序,使用几个不同的操作进行计算(可能效率非常低,但我离题了)。但是,在运行它时出现了一个错误,我无法理解。我认为这需要定义变量的类型。 课程:
import math
print('Select a number.')
y = input()
print('Select another number.')
x = input()
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)')
z = input()
if z == 'e' or z == 'E':
print('The answer is ' + y**x)
elif z == 'd' or z == 'D':
print('The answer is ' + y/z)
elif z == 'm' or z == 'M':
print('The answer is ' + y*x)
elif z == 'a' or z == 'A':
print('The answer is ' + y+x)
elif z == 's' or z == 'S':
print('The answer is ' + y-x)
elif z == 'mo' or z == 'Mo':
print('The answer is ' + y%x)
elif z == 'l' or z == 'L':
print('The answer is ' + math.log(x,y))
elif z == 'r' or z == 'R':
print('The answer is ' + y**(1/x))
shell中显示的错误:
Traceback (most recent call last):
File "C:/Users/UserNameOmitted/Downloads/Desktop/Python/Calculator.py", line 7, in <module>
z = input()
File "<string>", line 1, in <module>
NameError: name 'd' is not defined
你必须这么做
同样输入在
python2.x
中以int
的形式返回。所以使用raw_input
读取str
,然后像print('The answer is ' + y**x)
一样到处输入print('The answer is ' + str(y**x))
您的代码有错误
你的代码应该是这样的。你知道吗
这里有几个问题:
z
应该用^{input
。你知道吗y/z
,而不是y/x
。你知道吗相关问题 更多 >
编程相关推荐