老师布置的封装计算器的函数方法问题
问题描述:
封装计算器的函数方法,当calc_times 大于 1做非第一次运算的时候,请问如何能够带上calc_times = 1时第一次的计算结果参与运算?
相关代码:
calc_times = 1;
while True:
if calc_times == 1:
first_num = float(input("请输入第一个数:"));
operator = input("请输入操作符号:");
if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]:
right_operator = operator;
else:
print("您输入的操作符有误,请重新输入!");
continue;
if right_operator == "c" or right_operator == "C":
print("计算器清零,请重新输入!");
first_num = 0;
calc_times = 1;
continue;
elif right_operator == "q" or right_operator == "Q":
print("退出计算器运算!");
break;
second_num = float(input("请输入第二个数:"));
def calc(first_num , right_operator , second_num):
if right_operator == "+":
first_num += second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "-":
first_num -= second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "*":
first_num *= second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "/":
first_num /= second_num;
print("计算的结果是:" + str(first_num));
calc(first_num , right_operator , second_num);
calc_times += 1;
else:
operator = input("请输入操作符号:");
if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]:
right_operator = operator;
else:
print("您输入的操作符有误,请重新输入!");
continue;
if right_operator == "c" or right_operator == "C":
print("计算器清零,请重新输入!");
first_num = 0;
calc_times = 1;
continue;
elif right_operator == "q" or right_operator == "Q":
print("退出计算器运算!");
break;
second_num = float(input("请输入第二个数:"));
def calc1(first_num , right_operator , second_num):
if right_operator == "+":
first_num += second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "-":
first_num -= second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "*":
first_num *= second_num;
print("计算的结果是:" + str(first_num));
elif right_operator == "/":
first_num /= second_num;
print("计算的结果是:" + str(first_num));
calc1(first_num , right_operator , second_num);
calc_times += 1;
尝试过的解决方式:
如我写的代码,无法带入第一次的计算结果,请老师指点
12
收起
正在回答
2回答
同学,你好!同学代码存在以下几个问题:
1、第一次运算后的结果是result
第二次运行加减乘除时,应用第一次的result 值继续操作,同学第二次计算使用的是first_num第一次输入的数字,并且没有返回值。第二次调用函数时第一个参数应该写result
2、程序报错是因为第二次封装的函数中使用了result 变量,在该函数里result 属于局部变量,并不能获取第一次计算结果的result值,可以理解为没有定义变量,就使用,是错的。同学第二次定义函数时,将first_num改为result 就可了,将result 作为参数传递并使用
完整代码如下:
calc_times = 1; while True: if calc_times == 1: first_num = float(input("请输入第一个数:")); operator = input("请输入操作符号:"); if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]: right_operator = operator; else: print("您输入的操作符有误,请重新输入!"); continue; if right_operator == "c" or right_operator == "C": print("计算器清零,请重新输入!"); first_num = 0; calc_times = 1; continue; elif right_operator == "q" or right_operator == "Q": print("退出计算器运算!"); break; second_num = float(input("请输入第二个数:")); def calc(first_num , right_operator , second_num): if right_operator == "+": result = first_num + second_num; return result; elif right_operator == "-": result = first_num - second_num; return result; elif right_operator == "*": result = first_num * second_num; return result; elif right_operator == "/": result = first_num / second_num; return result; result = calc(first_num , right_operator , second_num); print("计算的结果是:" + str(result)); calc_times += 1; else: operator = input("请输入操作符号:"); if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]: right_operator = operator; else: print("您输入的操作符有误,请重新输入!"); continue; if right_operator == "c" or right_operator == "C": print("计算器清零,请重新输入!"); first_num = 0; calc_times = 1; continue; elif right_operator == "q" or right_operator == "Q": print("退出计算器运算!"); break; second_num = float(input("请输入第二个数:")); #def calc(first_num, right_operator, second_num): def calc(result , right_operator , second_num): if right_operator == "+": result = result + second_num; return result; elif right_operator == "-": result = result - second_num; return result; elif right_operator == "*": result = result * second_num; return result; elif right_operator == "/": result = result / second_num; return result; #calc(first_num , right_operator , second_num); result=calc(result, right_operator, second_num); print("计算的结果是:" + str(result)); calc_times += 1;
祝学习愉快~
泰斯塔罗莎
2023-08-26 06:19:41
calc_times = 1; while True: if calc_times == 1: first_num = float(input("请输入第一个数:")); operator = input("请输入操作符号:"); if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]: right_operator = operator; else: print("您输入的操作符有误,请重新输入!"); continue; if right_operator == "c" or right_operator == "C": print("计算器清零,请重新输入!"); first_num = 0; calc_times = 1; continue; elif right_operator == "q" or right_operator == "Q": print("退出计算器运算!"); break; second_num = float(input("请输入第二个数:")); def calc(first_num , right_operator , second_num): if right_operator == "+": result = first_num + second_num; return result; elif right_operator == "-": result = first_num - second_num; return result; elif right_operator == "*": result = first_num * second_num; return result; elif right_operator == "/": result = first_num / second_num; return result; result = calc(first_num , right_operator , second_num); print("计算的结果是:" + str(result)); calc_times += 1; else: operator = input("请输入操作符号:"); if operator in ["+" , "-" , "*" , "/" , "c" , "C" , "q" , "Q"]: right_operator = operator; else: print("您输入的操作符有误,请重新输入!"); continue; if right_operator == "c" or right_operator == "C": print("计算器清零,请重新输入!"); first_num = 0; calc_times = 1; continue; elif right_operator == "q" or right_operator == "Q": print("退出计算器运算!"); break; second_num = float(input("请输入第二个数:")); def calc(first_num , right_operator , second_num): if right_operator == "+": result = result + second_num; return result; elif right_operator == "-": result = result - second_num; return result; elif right_operator == "*": result = result * second_num; return result; elif right_operator == "/": result = result / second_num; return result; calc(first_num , right_operator , second_num); print("计算的结果是:" + str(result)); calc_times += 1;
尝试用了return改造了一下,第二次计算仍旧是报错,请老师指点
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星