亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我是否讓我的代碼對我自己來說太復雜了?

我是否讓我的代碼對我自己來說太復雜了?

Go
慕勒3428872 2023-02-22 15:05:36
我目前仍在學習 Python 的基礎知識。我看到其他人將攝氏度轉換為華氏度,這就像 10 行。雖然我的代碼超過 40 行。我沒有完全理解'返回'的意義是什么,所以我選擇了一個簡單的項目來理解它。但是當我意識到我的代碼對于如此簡單的事情來說太長了。感覺我應該做很多困難的項目,因為我在簡單的事情上使用了很多行。對不起,我是新手,我很想通過做項目來學習。困難的初學者項目的任何建議都會有所幫助。謝謝!import timedegree_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 adef celsius():    degree = int(input('\nHow many dgrees is it currently in Celsius?: '))    a = round((degree * 9/5) + 32)        return aprint("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
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

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()

d = {"c": f"{fahrenheit()}{degree_sign}C",

     "f": f"{celsius()}{degree_sign}F"}


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()

        try:

            time.sleep(0.5)

            print("It is currently ", d[weather])

        except:

            continue

同樣可以修改其他if-else子句


查看完整回答
反對 回復 2023-02-22
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號