Added week 6
This commit is contained in:
35
week6/ex6.py
Normal file
35
week6/ex6.py
Normal 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()
|
||||
Reference in New Issue
Block a user