IDE数据点能过,#3RE(Python)

B2052 简单计算器

tlm1587201497 @ 2021-10-07 10:24:33

a = input().split()
b = int(a[0])
c = int(a[1])
d = a[2]
if d != '+' and d != '-' and d != '/' and d != '*':
    print('Invalid operator!')
elif d == '+' or d == '-' or d == '/' or d == '*' and c != 0:
    if d == '+':
        a = b+c
        print("%.0f"%a)
    if d == '-':
        a = b-c
        print("%.0f"%a)
    if d == '*':
        a = b*c
        print("%.0f"%a)
    if d == '/':
        a = b/c
        print("%.0f"%a)
elif c == 0 and d == '/':
    print('Divided by zero!')

by gfgoodluck @ 2021-10-07 10:34:12

你应该把÷0的情况算在elif里了


by gfgoodluck @ 2021-10-07 10:35:04

and的优先级更高


by gfgoodluck @ 2021-10-07 10:36:19

把两个的顺序换一下,把最后一个改成else


by gfgoodluck @ 2021-10-07 10:38:06

a = input().split()
b = int(a[0])
c = int(a[1])
d = a[2]
if d != '+' and d != '-' and d != '/' and d != '*':
    print('Invalid operator!')
elif c == 0 and d == '/':
    print('Divided by zero!')
else:
    if d == '+':
        a = b+c
        print("%.0f"%a)
    if d == '-':
        a = b-c
        print("%.0f"%a)
    if d == '*':
        a = b*c
        print("%.0f"%a)
    if d == '/':
        a = b/c
        print("%.0f"%a)

by tlm1587201497 @ 2021-10-07 11:09:57

谢谢,过来


|