File tree Expand file tree Collapse file tree 1 file changed +38
-21
lines changed Expand file tree Collapse file tree 1 file changed +38
-21
lines changed Original file line number Diff line number Diff line change 1
- def add (a : object , b : object ) -> object :
2
- """
3
-
4
- :rtype: object
5
- """
6
- return a + b
7
-
8
- a = 3
9
- b = 4
10
- print (add (a ,b ))
11
-
12
- def subtract (c , d ):
13
- return c - d
14
-
15
-
16
- def multiply (e , f ):
17
- return e * f
18
-
19
-
20
- def divide (g , h ):
21
- return g / h
1
+ # 实现一个简单的计算器
2
+ # 1 接收用户输入的第一个数字(字符串类型),并转化为整数类型int
3
+ first_num = input ('请输入第一个数字:' )
4
+ first_num = int (first_num )
5
+ # 2 接收用户输入的运算符
6
+ symbol = input ('请输入运算符(+,-,*,/):' )
7
+ # 3 接收用户输入的第二个数字(字符串类型),并转化为整数类型int
8
+ sec_num = input ('请输入第二个数字:' )
9
+ sec_num = int (sec_num )
10
+ # 4 代码进行判断,计算
11
+ if symbol == '+' :
12
+ result = first_num + sec_num
13
+ print (f'{ first_num } { symbol } { sec_num } ={ result } ' )
14
+
15
+ elif symbol == '-' :
16
+ result = first_num - sec_num
17
+ print (f'{ first_num } { symbol } { sec_num } ={ result } ' )
18
+
19
+ elif symbol == '*' :
20
+ result = first_num * sec_num
21
+ print (f'{ first_num } { symbol } { sec_num } ={ result } ' )
22
+
23
+ elif symbol == '/' :
24
+ if sec_num == 0 :
25
+ result = '除数不能为0'
26
+ print (result )
27
+ else :
28
+ result = first_num / sec_num
29
+ print (f'{ first_num } { symbol } { sec_num } ={ result } ' )
30
+
31
+ else :
32
+ result = '输入错误'
33
+ print (result )
34
+
35
+ # 5 显示计算的结果,以及完整的等式
36
+ # 格式化输出 format
37
+ # print(result)
38
+ print (f'{ first_num } { symbol } { sec_num } ={ result } ' )
You can’t perform that action at this time.
0 commit comments