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

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

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('. .')

英文:
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('.        .')

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(),它与浮点数和整数都兼容。

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

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

a = float(input('Enter 1st number: '))
b = input('Enter operator:')
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:

确定