第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我在使用兩個(gè)函數(shù)編碼時(shí)遇到了一些問題

我在使用兩個(gè)函數(shù)編碼時(shí)遇到了一些問題

慕慕森 2022-12-20 16:30:17
我正在嘗試在 python 上制作一個(gè)貨幣計(jì)算器:print("Please choose which currency you want to convert:")print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")print("E - Quit ")A=0B=0C=0D=0usd = 0.000905eur = 0.000807350908yen = 0.0919061643rmb = 0.00603703605def main():    (option, amount) = Input()    Output(totalamount)def Input():    option = eval(input("Enter your option: "))    amount = eval(input("Enter the amoutn in Korean Won: "))    if option == "A":        totalamount = (amount * usd)        print (amount +"Won equals to "+totalamount+" USD")    elif option== "B":        totalamount = (amount * eur)        print (amount +"Won equals to "+totalamount+" Euro")    elif option== "C":        totalamount = (amount * yen)        print (amount +"Won equals to "+totalamount+" Yen")    elif option== "D":        totalamount = (amount * rmb)        print (amount +"Won equals to "+totalamount+" Chinese RMB")    else:        quitmain()我仍在學(xué)習(xí)如何使用 python,但我想知道為什么每次運(yùn)行該程序時(shí)都會(huì)出現(xiàn)此錯(cuò)誤:TypeError: cannot unpack non-iterable NoneType object我該如何解決這個(gè)問題?
查看完整描述

3 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

def main():

    (option, amount) = Input()

    Output(totalamount)

這里要分配的值 option和amount變量應(yīng)由 input() 函數(shù)提供,但如您所見,您的輸入函數(shù)沒有返回值的地方,因此默認(rèn)情況下返回None


您期望返回一些值。


這是你的完整代碼



print("Please choose which currency you want to convert:")

print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")

print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")

print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")

print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")

print("E - Quit ")


A=0

B=0

C=0

D=0


usd = 0.000905

eur = 0.000807350908

yen = 0.0919061643

rmb = 0.00603703605

def main():

    (option, amount,totalamount) = Input()  #modified

    print(totalamount)  



def Input():

    option = input("Enter your option: ")

    amount = eval(input("Enter the amoutn in Korean Won: "))

    totalamount=0  #added here

    if option == "A":

        totalamount = (amount * usd)

        print (str(amount) +"Won equals to "+str(totalamount)+" USD")

    elif option== "B":

        totalamount = (amount * eur)

        print ("{} Won equals to {} Euro".format(amount,totalamount))

    elif option== "C":

        totalamount = (amount * yen)

        print ("{} Won equals to {} Yen".format(amount,totalamount))

    elif option== "D":

        totalamount = (amount * rmb)

        print ("{} Won equals to {} Chinese RMB".format(amount,totalamount))

    else:

        quit


    return option,amount,totalamount



main()



查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

您沒有返回任何內(nèi)容并將輸出None放在兩個(gè)不同的變量中。這是不對(duì)的。在函數(shù)末尾添加

return option, amount


查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

這里有一些建議/和修復(fù)。還有很多東西可以改進(jìn),但我想展示一些基本的東西,希望它們不會(huì)太復(fù)雜而難以理解,但可以使您的代碼更好。


這個(gè)想法是編寫易于閱讀和更改的代碼。


usd = 0.000905

eur = 0.000807350908

yen = 0.0919061643

rmb = 0.00603703605



def print_choices():

    """

    This is a doc string. Here you should describe what your function is doing.


    This function prints the choices, that a user can make

    """

    print("Please choose which currency you want to convert:")

    # DRY: (=Don't Repeat Yourself). you have the exchange rate already in

    # variables. so use them, so that if the exchange rate changes you 

    # need to change only one line in your code.

    print("A - Korean Won to US Dollar (Exchange Rate: %f)" % usd)

    print("B - Korean Won to Euro (Exchange Rate: %f)" % eur)

    print("C - Korean Won to Japanese Yen (Exchange Rate: %f)" % yen)

    print("D - Korean Won to Chinese RMB (Exchange Rate: %f)" % rmb)

    print("E - Quit")

    # describe what happens if you enter another value

    # lateron you might want to rewrite your code such, that it rejects 

    # any option, that is not A, B, C, D, E and asks for input until it is

    # correct.

    print("any other input will Quit ")


# variables A,B,C,D are not used in your code, so remove them


# function names should only use lowercase characters and '_' 

# This is a way of telling others, that this is a variable or a function

# It is got practice, that function names start with a verb and then an object.

def get_user_input():

    """ 

    This function prompts the user for an option and an amount in

    Korean Won and returns it to the caller.


    returns: options, amount

    """


    option = input("Enter your option: ")

    # convert option to uppercase, so even if the user enters 'a', 

    # option 'A' will be chosen

    option = option.upper()


    # eval should not be used it is a dangerous function. Use float instead

    amount = float(input("Enter the amount in Korean Won: "))

    return option, amount


def calculate_and_print_converted_amount(option, amount):

    """ 

    depending on option, amount is converted to another currency

    This function calculates and displays the converted amount

    and the currency. 

    If option "E" is selected Nothing is displayed.

    if an unknown option is selected a small warning will be displayed

    and no currency conversion is performed.

    """


    if option == "A":

        conversionrate = usd

        currency = "USD"

    elif option == "B":

        conversionrate = eur

        currency = "Euro"

    elif option== "C":

        conversionrate = yen

        currency = "Yen"

    elif option== "D":

        conversionrate = rmb

        currency = "Chinese RMB"

    elif option== "E":

        return

    else:

        # quit is no python command use return instead to return from

        # a function.

        # alternatively you can exit the entire python script 

        # with sys.exit()

        print("unknown option %s. Will quit" % option)

        return

    # Use only one print statement, so you can change formatting by changin

    # only one line

    totalamount = amount * conversionrate

    print ("%.2f Won equals to %.2f %s" % (amount, totalamount, currency))



def main():

    print_choices()

    (option, amount) = get_user_input()

    calculate_and_print_converted_amount(option, amount)

    # if you wanted to you could have one function (calculate_amount), 

    # that just calculates the amount and returns the amount and the 

    # currency and another function printing the answer.

    # you see that many programs separate 'calculation' from 'presentation'

    # one function does the work, the other one decides how to 'present' 

    # the information. (print to the terminal, write to a file, display 

    # with a GUI framework.

    # If you'd separate code like that you could re use the calculation if

    # you change your user interface and you just had to reimplement the

    # function presenting the result



main()



查看完整回答
反對(duì) 回復(fù) 2022-12-20
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)