老师布置的封装计算器的函数方法问题

老师布置的封装计算器的函数方法问题

问题描述:

封装计算器的函数方法,当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;

尝试过的解决方式:

如我写的代码,无法带入第一次的计算结果,请老师指点

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学,你好!同学代码存在以下几个问题:

1、第一次运算后的结果是result 

https://img1.sycdn.imooc.com//climg/64e94109098f0c5e10230066.jpg

第二次运行加减乘除时,应用第一次的result 值继续操作,同学第二次计算使用的是first_num第一次输入的数字,并且没有返回值。第二次调用函数时第一个参数应该写result

https://img1.sycdn.imooc.com//climg/64e940ac0921505909940103.jpg

2、程序报错是因为第二次封装的函数中使用了result 变量,在该函数里result 属于局部变量,并不能获取第一次计算结果的result值,可以理解为没有定义变量,就使用,是错的。同学第二次定义函数时,将first_num改为result 就可了,将result 作为参数传递并使用

https://img1.sycdn.imooc.com//climg/64e9430a09d95b7809150250.jpg

完整代码如下:

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改造了一下,第二次计算仍旧是报错,请老师指点

https://img1.sycdn.imooc.com//climg/64e928f209df00dc09370180.jpg

  • 同学,你好!同学可以简化下代码。函数是为了方便多次使用,同学第一次计算、第二次计算功能都是相同的,可以只写一个函数,多次调用传参正确就可以的,优化代码如下:

    calc_times = 1;
    
    
    def calc(num, right_operator, second_num):
        if right_operator == "+":
            result = num + second_num;
            return result;
        elif right_operator == "-":
            result = num - second_num;
            return result;
        elif right_operator == "*":
            result = num * second_num;
            return result;
        elif right_operator == "/":
            result = num / second_num;
            return result;
    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("请输入第二个数:"));
            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("请输入第二个数:"));
            result=calc(result, right_operator, second_num);
            print("计算的结果是:" + str(result));
            calc_times += 1;

    祝学习愉快~

    2023-08-26 08:19:22
  • 提问者 泰斯塔罗莎 回复 好帮手慕小猿 #2

    问题解决,谢谢老师

    2023-08-26 08:45:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师