# python 编写程序求大佬帮忙解答一下

0
[待解决问题]

编写程序实现“计算器”功能，输入两个运算数x、y和运算符，实现 加、减、乘、除四则运算，当进行除尘运算时，若除数为0，则显示“除数不能为0！”。

0

Sunny-King | 园豆：355 (菜鸟二级) | 2023-04-13 08:58

0

# 定义函数，实现加、减、乘、除四则运算

def calculator(x, y, operator):
if operator == '+':
return x + y
elif operator == '-':
return x - y
elif operator == '*':
return x * y
elif operator == '/':
if y == 0:
print("除数不能为0！")
return
else:
return x / y
else:
print("不支持的运算符！")

# 测试程序

print("请选择运算符：")
print("1.加")
print("2.减")
print("3.乘")
print("4.除")

choice = input("请输入你的选择（1/2/3/4）：")
num1 = float(input("请输入第一个数字："))
num2 = float(input("请输入第二个数字："))

if choice == '1':
result = calculator(num1, num2, '+')
print("{0} {1} = {2}".format(num1, num2, result))
elif choice == '2':
result = calculator(num1, num2, '-')
print("{0} {1} = {2}".format(num1, num2, result))
elif choice == '3':
result = calculator(num1, num2, '*')
print("{0} {1} = {2}".format(num1, num2, result))
elif choice == '4':
if num2 == 0:
print("除数不能为0！")
return
else:
result = calculator(num1, num2, '/')
print("{0} {1} = {2}".format(num1, num2, result))
else:
print("不支持的选择！")

0

# 获取用户输入的两个运算数和运算符

x = float(input("请输入第一个运算数："))
y = float(input("请输入第二个运算数："))
operator = input("请输入运算符（+、-、*、/）：")

# 根据运算符进行相应的运算

if operator == "+":
result = x + y
elif operator == "-":
result = x - y
elif operator == "*":
result = x * y
elif operator == "/":
if y == 0:
print("除数不能为0！")
else:
result = x / y
else:
print("无效的运算符！")

# 输出运算结果

if operator in ["+", "-", "*", "/"]:
print(f"{x} {operator} {y} = {result}")

0

python

x = float(input("请输入第一个数："))
y = float(input("请输入第二个数："))
operator = input("请输入运算符（+、-、*、/）：")

if operator == "+":
result = x + y
elif operator == "-":
result = x - y
elif operator == "*":
result = x * y
elif operator == "/":
if y == 0:
print("除数不能为0！")
else:
result = x / y
else:
print("输入的运算符不合法！")

print("运算结果为：", result)

Technologyforgood | 园豆：3661 (老鸟四级) | 2023-04-13 21:36
0

# take user input for x, y, and operator

x = float(input("请输入第一个数："))
y = float(input("请输入第二个数："))
operator = input("请输入运算符（+、-、*、/）：")

# define a function to perform the calculator operation

def calculator(x, y, operator):
if operator == '+':
return x + y
elif operator == '-':
return x - y
elif operator == '*':
return x * y
elif operator == '/':
if y == 0:
return "除数不能为0！"
else:
return x / y

# call the calculator function and print the result

result = calculator(x, y, operator)
print("计算结果为：", result)

Dear-Fantasy | 园豆：202 (菜鸟二级) | 2023-04-27 23:15
0

# 当进行除尘运算时，若除数为0，则显示“除数不能为0！”。

x=input() #操作数x，y,运算符 +，-，*，/
y=input()
x=int(x)
y=int(y)
symbox=input()

# 对比字符串

result1=x+y
result2=x-y
result3=xy
result4=x/y
if symbox == "+":
print(result1)
elif symbox == "-":
print(result2)
elif symbox == "
":
print(result3)
elif symbox == "/":
if y==0:
print("除数不能为0")
else:
print(result4)
else:
print("你的输入有误")

您需要登录以后才能回答，未注册用户请先注册