【发布时间】:2020-02-29 07:37:15
【问题描述】:
我有 2 个问题。 我的第一个问题是我不知道如何编写整数代码。输出应该写成一个整数......但是当我使用例如 4.3+5 它不起作用......答案应该是“简单” 9...... ValueError:以 10 为底的 int() 的无效文字: 谁能帮我写这个小计算器?
operation = input('Select an operation (type "addition",
"subtraction", "multiplication", or "division" and hit enter)')
datatype = input('Select a datatype (type "int" or "float" and hit
enter)')
value1 = input('Value 1:')
value2 = input('Value 2:')
result = None # This variable should be overwritten with the result of
your operation later.
if datatype == "float":
value1 = float(value1)
value2 = float(value2)
else:
value1 = int(value1)
value2 = int(value2)
if operation == "addition":
result = value1+value2
elif operation == "subtraction":
result = value1-value2
elif operation == "multiplication":
result = value1*value2
elif operation == "division":
result = value1/value2
print(f"Result: {result}")
这是输出中的一些其他问题 --> 它总是向我显示整个计算...但我只想要解决方案。
输出是:
Select an operation (type "addition", "subtraction", "multiplication",
or "division" and hit enter)multiplication
Select a datatype (type "int" or "float" and hit enter)float
Value 1:123456789123456789123456789
Value 2:-4.3
123456789123456789123456789 * -4.3 =
Result: -5.3086419323086415e+26
输出应该是:
Select an operation (type "addition", "subtraction",
"multiplication", or "division" and hit enter)multiplication
Select a datatype (type "int" or "float" and hit enter)float
Value 1:123456789123456789123456789
Value 2:-4.3
Result: -5.3086419323086415e+26
【问题讨论】:
-
为什么
4.3+5的答案应该是9?4.5+5呢?4.99999+5?