gpt4 book ai didi

python - 我做的python计算器出错

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:11 25 4
gpt4 key购买 nike

我试图用 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

最佳答案

这里有几个问题:

  1. z 应输入 raw_input ,而不是输入
  2. 在除法的情况下,您要除的是 y/z 而不是 y/x

关于python - 我做的python计算器出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36219303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com