我正在开发我的计算器,它无法处理浮点数。

huangapple go评论96阅读模式
英文:

I am working on my calculator and it can't do floats

问题

e = 'p'
def sum(a, b):
return (a + b)
def sub(a, b):
return (a - b)
def mul(a, b):
return (a * b)
def div(a, b):
return (a / b)
while True:
a = int(input('Enter 1st number: '))
b = input('Enter operator:')
c = int(input('Enter 2nd number: '))
if b == '+':
d = sum
e = 'sum'
if b == '-':
d = sub
e = 'difference'
if b == '*':
d = mul
e = 'product'
if b == '/':
d = div
e = 'quotient'
print('the ' +e+ f' of {a} and {c} is {d(a, c)}')
print('. .')

英文:
  1. e = 'p'
  2. def sum(a, b):
  3. return (a + b)
  4. def sub(a, b):
  5. return (a - b)
  6. def mul(a, b):
  7. return (a * b)
  8. def div(a, b):
  9. return (a / b)
  10. while True:
  11. a = int(input('Enter 1st number: '))
  12. b = input('Enter operator:')
  13. c = int(input('Enter 2nd number: '))
  14. if b == '+':
  15. d = sum
  16. e = 'sum'
  17. if b == '-':
  18. d = sub
  19. e = 'difference'
  20. if b == '*':
  21. d = mul
  22. e = 'product'
  23. if b == '/':
  24. d = div
  25. e = 'quotient'
  26. print('the ' +e+ f' of {a} and {c} is {d(a, c)}')
  27. print('. .')

it simply can't do floats
and I need it to do floats because
I want it to do floats and integers

答案1

得分: 1

替代使用int(),您可以使用float(),它与浮点数和整数都兼容。

  1. a = float(input('输入第一个数字: '))
  2. b = input('输入运算符:')
  3. c = float(input('输入第二个数字: '))
英文:

Instead of using int(), you can use float() which is compatible with both floats and integers.

  1. a = float(input('Enter 1st number: '))
  2. b = input('Enter operator:')
  3. c = float(input('Enter 2nd number: '))

huangapple
  • 本文由 发表于 2023年2月6日 06:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定