我目前仍在学习 Python 的基础知识。我看到其他人将摄氏度转换为华氏度,这就像 10 行。虽然我的代码超过 40 行。我没有完全理解'返回'的意义是什么,所以我选择了一个简单的项目来理解它。但是当我意识到我的代码对于如此简单的事情来说太长了。感觉我应该做很多困难的项目,因为我在简单的事情上使用了很多行。
对不起,我是新手,我很想通过做项目来学习。困难的初学者项目的任何建议都会有所帮助。谢谢!
import time
degree_sign = u'\N{DEGREE SIGN}'
def fahrenheit():
degree = int(input('\nHow many degrees is it currently in Fahrenheit?: '))
a = round((degree - 32) * 5/9)
return a
def celsius():
degree = int(input('\nHow many dgrees is it currently in Celsius?: '))
a = round((degree * 9/5) + 32)
return a
print("Welcome to my first weather conversion!\n")
weather = input('Do you want to convert to Fahrenheit or Celsius (c/f)? \n').lower()
if weather == "c":
time.sleep(0.5)
print(f'It is currently {fahrenheit()}{degree_sign}C.')
elif weather == "f":
time.sleep(0.5)
print(f'\nIt is currently {celsius()}{degree_sign}F.')
else:
while True:
print("\nI'm sorry I don't understand.\n")
weather = input('Do you want to convert to Fahrenheit or Celsius (c/f)? \n').lower()
if weather != "c" and weather != "f":
continue
elif weather == "c":
time.sleep(0.5)
print(f'\nIt is currently {fahrenheit()}{degree_sign}C')
break
elif weather == "f":
time.sleep(0.5)
print(f'\nIt is currently {celsius()}{degree_sign}F.')
break
杨__羊羊
相关分类