Added week 6

This commit is contained in:
2026-04-13 09:45:45 +08:00
parent 923c041dd6
commit 9b90ac4340
7 changed files with 139 additions and 0 deletions

11
week6/ex1.py Normal file
View File

@@ -0,0 +1,11 @@
def main():
print('==Exercise 1==')
name = input("Please input your name > ")
print("Welcome on board 007." if name=="Bond" else f"Good morning {name}.")
print('==End Of Ex1==')
return 0
if __name__ == '__main__':
main()

11
week6/ex2.py Normal file
View File

@@ -0,0 +1,11 @@
def main():
print('==Exercise 2==')
name = input("Please input your name > ")
print("Welcome on board 007." if name.lower()=="Bond".lower() else f"Good morning {name}.")
print('==End Of Ex2==')
return 0
if __name__ == '__main__':
main()

21
week6/ex3.py Normal file
View File

@@ -0,0 +1,21 @@
def main():
print('==Exercise 3==')
flag=True
while flag:
a = input("pls input a number:")
try:
a = int(a)
flag=False
except :
print("pls input a number!")
print(f"Test if the number {a} is even: {True if a%2==0 else False}")
#我其实挺想直接a%2==0的不过尊重一下本节课的主题
print('==End Of Ex3==')
return 0
if __name__ == '__main__':
main()

24
week6/ex4.py Normal file
View File

@@ -0,0 +1,24 @@
from decimal import Decimal
#引入decimal模块是因为python不是默认高精浮点数
def main():
print('==Exercise 4==')
flag=True
while flag:
a = input("pls input a decimal:")
try:
a = Decimal(a)
flag=False
except :
print("pls input a decimal!")
d = a-int(a)
print(f"Decimal Part: {d if d else 'INTEGER'}")
print('==End Of Ex4==')
return 0
if __name__ == '__main__':
main()

30
week6/ex5.py Normal file
View File

@@ -0,0 +1,30 @@
from decimal import Decimal
#引入decimal模块是因为python不是默认高精浮点数
def main():
print('==Exercise 5==')
flag=True
while flag:
a = input("number1=")
try:
a = Decimal(a)
flag=False
except :
print("pls input a number!")
flag=True
while flag:
b = input("number2=")
try:
b = Decimal(b)
flag=False
except :
print("pls input a number!")
print(f"The result is {a+b if a*b>Decimal(1000.0) else a*b}")
print('==End Of Ex5==')
return 0
if __name__ == '__main__':
main()

35
week6/ex6.py Normal file
View File

@@ -0,0 +1,35 @@
from decimal import Decimal
#引入decimal模块是因为python不是默认高精浮点数
def main():
print('==Exercise 6==')
i = input("Input the temperature you like to convert? (e.g., 45F, 102C etc.) :").lower()
ins = ""
use_celsius = False
for ii in i:
if ii in '1234567890.':
ins+=ii
elif ii in 'cf':
use_celsius = ii == 'c'
break
else:
print(f"Unexcepted char {ii}! Ignored.")
if len(ins)!=len(i)-1:
print(f"WARNING! content below \'c\' or \'f\' is ignored! (using {ins} {'c' if use_celsius else 'f'})")
ins = Decimal(ins)
#c/5 = f-32/9
#f = c*9/5+32
#c = (f-32)*5/9
o = ins*9/5+Decimal(32.0) if use_celsius else (ins-Decimal(32.0))*5/9
print(f"The temperature in {'Fahrenheit' if use_celsius else 'Celsius'} is {o} degrees.")
print('==End Of Ex6==')
return 0
if __name__ == '__main__':
main()

7
week6/run_all.py Normal file
View File

@@ -0,0 +1,7 @@
import ex1,ex2,ex3,ex4,ex5,ex6
ex1.main()
ex2.main()
ex3.main()
ex4.main()
ex5.main()
ex6.main()