章节 4-8 操作符判断怎么写,课程别讲一半不说了呀

章节 4-8 操作符判断怎么写,课程别讲一半不说了呀

源代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
1、输入了一个数字,然后输入操作符号,然后输入一个数字,然后就算出结果
2、输入了一个数字,然后输入操作符号,然后输入一个数字,然后输入操作符号,然后输入一个数字..
然后就算出结果
 
"""
# 声明一个变量,来记录到底是第几次运算
number_of_times = 1
while True:
    # 判断到底是不是第一次计算
    if number_of_times == 1:
        result = input("请输入第一个数字:")
        operator = input("请输入操作符号:")
        if operator == "q":
            print("计算程序退出")
            break
        elif operator == "c":
            print("计算器清零,重新开始计算")
            result = 0
            number_of_times = 1
            continue
        second_number = input("请输入第二个数字:")
        result = float(result)
        second_number = float(second_number)
        if operator == "+":
            result += second_number
        elif operator == "-":
            result -= second_number
        elif operator == "*":
            result *= second_number
        elif operator == "/":
            result /= second_number
        else:
            print("没有匹配到正确的操作符,操作符号有错误")
 
        print("计算的结果是:"+ str(result))
        number_of_times += 1
    else:
        operator = input("请输入操作符号:")
        if operator == "q":
            print("计算程序退出")
            break
        elif operator == "c":
            print("计算器清零,重新开始计算")
            result = 0
            number_of_times = 1
            continue
        second_number = input("请输入第二个数字:")
        result = float(result)
        second_number = float(second_number)
        if operator == "+":
            result += second_number
        elif operator == "-":
            result -= second_number
        elif operator == "*":
            result *= second_number
        elif operator == "/":
            result /= second_number
        else:
            print("没有匹配到正确的操作符,操作符号有错误")
 
        print("计算的结果是:"+ str(result))
        number_of_times += 1


正在回答 回答被采纳积分+1

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

1回答
好帮手慕小猿 2023-07-04 10:26:49

同学,你好!可以对输入的操作符进行判断,看操作符是否在特定的运算符内。若是在运算符内,对操作符进行最终赋值,不在操作符内则进行下一次循环

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

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

完整参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
number_of_times = 1
while True:
    # 判断到底是不是第一次计算
    if number_of_times == 1:
        result = input("请输入第一个数字:")
        operator = input("请输入操作符号:")
        if operator  in ["+","-","*","/"]:
            final_operator=operator
        else:
            print("没有匹配到正确的操作符,操作符号有错误")
            continue
        if operator == "q":
            print("计算程序退出")
            break
        elif operator == "c":
            print("计算器清零,重新开始计算")
            result = 0
            number_of_times = 1
            continue
        second_number = input("请输入第二个数字:")
        result = float(result)
        second_number = float(second_number)
        if final_operator == "+":
            result += second_number
        elif final_operator == "-":
            result -= second_number
        elif final_operator == "*":
            result *= second_number
        elif final_operator == "/":
            result /= second_number
        else:
            print("没有匹配到正确的操作符,操作符号有错误")
            continue
        print("计算的结果是:" + str(result))
        number_of_times += 1
    else:
        operator = input("请输入操作符号:")
        if operator  in ["+","-","*","/"]:
            final_operator=operator
        else:
            print("没有匹配到正确的操作符,操作符号有错误")
            continue
        if operator == "q":
            print("计算程序退出")
            break
        elif operator == "c":
            print("计算器清零,重新开始计算")
            result = 0
            number_of_times = 1
            continue
        second_number = input("请输入第二个数字:")
        result = float(result)
        second_number = float(second_number)
        if final_operator=="+":
            result+=second_number
        elif final_operator == "-":
            result -= second_number
        elif final_operator == "*":
            result *= second_number
        elif final_operator == "/":
            result /= second_number
        print("计算的结果是:" + str(result))
        number_of_times += 1

祝学习愉快~

  • 提问者 怒焰狂暴 #1

    o ,if...in的语法还没江街道,难怪老师不说,好哒...知道了

    2023-07-04 11:06:50
  • 这个还是有明显的bug,输入q提示没有匹配到正确的操作符,操作符号有错误

    2023-08-14 10:28:54
  • 同学,你好!同学可参考以下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    number_of_times = 1
    while True:
        # 判断到底是不是第一次计算
        if number_of_times == 1:
            result = input("请输入第一个数字:")
            operator = input("请输入操作符号:")
            if operator == "q":
                print("计算程序退出")
                break
            if operator == "c":
                print("计算器清零,重新开始计算")
                result = 0
                number_of_times = 1
                continue
            if operator  in ["+","-","*","/"]:
                final_operator=operator
            else:
                print("没有匹配到正确的操作符,操作符号有错误,请重新输入数字及操作符")
                continue
     
            second_number = input("请输入第二个数字:")
            result = float(result)
            second_number = float(second_number)
            if final_operator == "+":
                result += second_number
            elif final_operator == "-":
                result -= second_number
            elif final_operator == "*":
                result *= second_number
            elif final_operator == "/":
                result /= second_number
            else:
                print("没有匹配到正确的操作符,操作符号有错误")
                continue
            print("计算的结果是:" + str(result))
            number_of_times += 1
        else:
            operator = input("请输入操作符号:")
     
            if operator == "q":
                print("计算程序退出")
                break
            if operator == "c":
                print("计算器清零,重新开始计算")
                result = 0
                number_of_times = 1
                continue
            if operator  in ["+","-","*","/"]:
                final_operator=operator
            else:
                print("没有匹配到正确的操作符,操作符号有错误")
                continue
            second_number = input("请输入第二个数字:")
            result = float(result)
            second_number = float(second_number)
            if final_operator=="+":
                result+=second_number
            elif final_operator == "-":
                result -= second_number
            elif final_operator == "*":
                result *= second_number
            elif final_operator == "/":
                result /= second_number
            print("计算的结果是:" + str(result))
            number_of_times += 1

    祝学习愉快~

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

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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