Python课堂笔记之实现一个数字四则运算

it2025-09-23  6

实现一个数字四则运算

1、

def multiply(a,c,b): if c=='+': print(a,b,a+b) elif c=='-': print(a,b,a-b) elif c=='*': print(a,b,a*b) elif c=='/': if b==0: print("error!") else: print(a,b,a/b) elif c!='+' or c!='-' or c!='*' or c!='/': print("error operator!") elif not a.isdigit() or b.isdigit(): print("error!") multiply(4,'+',7) multiply(4,'-',1) multiply(4,'*',7) multiply(14,'/',7) multiply(14,'/',0) multiply(14,'%',7) 4 7 11 4 1 3 4 7 28 14 7 2.0 error! error operator!

2、

a = int(input("")) b = int(input("")) symbol = input("") if symbol == "+": print("%s%s%s=%s"%(a,symbol,b,a+b)) elif symbol == "-": print("%s%s%s=%s"%(a,symbol,b,a-b)) elif symbol == "*": print("%s%s%s=%s"%(a,symbol,b,a*b)) elif symbol == "/": if b == 0: print("error operator!error operator!") else:print("%s%s%s=%s"%(a,symbol,b,a/b)) else: print("error operator!") 5 9 * 5*9=45 ***Repl Closed***

3、

from d import * print("请选择四则运算类型:(+,-,*,/)") i=input() while True: if i!="+" and i!="-" and i!="*" and i!="/": print("i必须为四则运算符!input again") print("请选择四则运算类型:(+,-,*,/)") i=input() else: print("请输入第一个数:") c=input() print("请输入第二个数:") d=input() if c.isdigit() and d.isdigit(): c=int(c) d=int(d) if i=="+": addition(c,d) elif i=="-": minus(c,d) elif i=="*": multiply(c,d) elif i=="/": division(c,d) print("请选择四则运算类型:(+,-,*,/)") i=input() else: print("error,please input number!")
最新回复(0)