python计算器中整数的问题

我有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",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",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
A008888 回答:python计算器中整数的问题

您输入“ Result:result”时输入方程式的原因。这导致方程式的打印。您可以通过以下方式进行编辑:

print("result")

这是我的计算器代码。它很短,但可以输入小数。

print("Calculator")

operation = (input("Please choose an operator sign:"))

num1 = float(input("Please put in your first number:"))
num2 = float(input("Please put in your second number:"))

if operation == '+':
  ans = num1 + num2
  print("Your answer is",ans)
elif operation == '-':
  ans = num1 - num2
  print("Your answer is",ans)
elif operation == '*':
  ans = num1 * num2
  print("Your answer is",ans)
elif operation == '/':   
  if num2 == 0:
    print("We cannot divide this number by 0.")
  else:
    ans = num1 / num2
    print("Your answer is",ans)
本文链接:https://www.f2er.com/3169671.html

大家都在问