-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
def add(a: object, b: object) -> object: | ||
""" | ||
:rtype: object | ||
""" | ||
return a + b | ||
|
||
a=3 | ||
b=4 | ||
print(add(a,b)) | ||
|
||
def subtract(c, d): | ||
return c - d | ||
|
||
|
||
def multiply(e, f): | ||
return e * f | ||
|
||
|
||
def divide(g, h): | ||
return g / h | ||
# 实现一个简单的计算器 | ||
# 1 接收用户输入的第一个数字(字符串类型),并转化为整数类型int | ||
first_num = input('请输入第一个数字:') | ||
first_num = int(first_num) | ||
# 2 接收用户输入的运算符 | ||
symbol = input('请输入运算符(+,-,*,/):') | ||
# 3 接收用户输入的第二个数字(字符串类型),并转化为整数类型int | ||
sec_num = input('请输入第二个数字:') | ||
sec_num = int(sec_num) | ||
# 4 代码进行判断,计算 | ||
if symbol == '+': | ||
result = first_num + sec_num | ||
print(f'{first_num}{symbol}{sec_num}={result}') | ||
|
||
elif symbol == '-': | ||
result = first_num - sec_num | ||
print(f'{first_num}{symbol}{sec_num}={result}') | ||
|
||
elif symbol == '*': | ||
result = first_num * sec_num | ||
print(f'{first_num}{symbol}{sec_num}={result}') | ||
|
||
elif symbol == '/': | ||
if sec_num == 0: | ||
result = '除数不能为0' | ||
print(result) | ||
else: | ||
result = first_num / sec_num | ||
print(f'{first_num}{symbol}{sec_num}={result}') | ||
|
||
else: | ||
result = '输入错误' | ||
print(result) | ||
|
||
# 5 显示计算的结果,以及完整的等式 | ||
# 格式化输出 format | ||
# print(result) | ||
print(f'{first_num}{symbol}{sec_num}={result}') |
c867408
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自学训练营学习2群Day03
@liujiayi0042