0

私は Python プログラミング コースを行っており、それを実現する方法について頭を悩ませようとしています。私はいくつかのコードを書き、ポップアップするエラーを修正しようとしていますが、混乱して何も解決しないので、皆さんに頼ります. 私がこれまでに書いてきたことを見て、アイデアや提案が浮かんだら幸いです。特に、$2 より上または下の値を取得する必要がある最後の部分をどのように実行するかを理解するのに苦労しています。

私はこの演習を行っています:

ちょうど 2 ドルを稼ぐのに必要なコインの枚数をユーザーに入力させるおつりカウント ゲームを作成します。ユーザーに 5 セント硬貨、10 セント硬貨、20 セント硬貨、50 セント硬貨、1 ドル硬貨、2 ドル硬貨を入力するよう求める Python プログラムを実装します。入力されたこれらのコインの合計値が 2 ドルに等しい場合、プログラムはユーザーがゲームに勝ったことを祝福する必要があります。それ以外の場合、プログラムは、合計が正確に 2 ドルではないことを通知し、値が 2 ドルを上回るか下回るかを示すメッセージを表示する必要があります。

更新: 最後の関数にいくつかの変更を加えましたが、完全に正常に機能しました。

#Global Variables

v_five = float(0.05)
v_ten = float(0.10)
v_twenty = float(0.20)
v_fifty = float(0.50)
v_one_dollar = int(1)
v_two_dollar = int(2)
dollar = 0

def main():

    """The main function defines the variables that are needed by taking input
    from the user. The main() function is calling all the other functions one 
    by one to execute their intended commands and give the results"""

    intro() #Displays the rules of the game

    #Takes input from the user. One input per denomination
    five=float(input(" Enter number of FIVE CENT coins: "))
    ten=float(input(" Enter number of TEN CENT coins: "))
    twenty=float(input(" Enter number of TWNETY CENT coins: "))
    fifty=float(input(" Enter the number of FIFTY CENT coins: "))
    one_dollar=int(input(" Enter the number of ONE DOLLAR coins: "))
    two_dollar=int(input(" Enter the number of TWO DOLLAR coins: "))

    #Shows what the user entered
    show_change(five,ten,twenty,fifty,one_dollar,two_dollar)
    #Converts the value of the total into dollars and cents from 
    #what the user has entered
    calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar)
    #Calculates and Prints the total along with what the final number
    #was
    #CalculateAndPrint(five,ten,twenty,fifty,one_dollar,two_dollar)
    CalculateAndPrint(dollar)
def intro():

    """This function simply prints out the instructions for the user"""

    print("")
    print(" Welcome to the Coin Change game!")
    print(" Enter a number for each denomination below")
    print(" Your total should be $2 and no more.")
    print(" Good Luck!\n")   

def show_change(five,ten,twenty,fifty,one_dollar,two_dollar):

    """This function shows what the user has entered after taking input from
    the user"""

    print("")
    print(" You entered: \n\n {} five cent(s) \n {} ten cent(s) \n {} twenty cent(s) \n {} fifty cent(s) \n {} one dollar \n {} two dollar coins".format(five,ten,twenty,fifty,one_dollar,two_dollar))

def calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar):

    """This function will convert the entered values into cents so that they
    can be calculated and checked if they exceed the $2 amount."""

    fiveAmount = v_five * five
    tenAmount = v_ten * ten
    twentyAmount = v_twenty * twenty
    fiftyAmount = v_fifty * fifty
    oneAmount = v_one_dollar * one_dollar
    twoAmount = v_two_dollar * two_dollar

    global dollar
    dollar = fiveAmount + tenAmount + twentyAmount + fiftyAmount + oneAmount + twoAmount

    """This function checks whether the total was over or under $2 and displays a 
    win or loose message for the user. Also shows the total that the user entered"""

def CalculateAndPrint(dollar):
    if dollar == 2.00:#Checks if the dollar value being passed from the previous function
        #is 2 or not
        print(" \n Congratulations! You've hit a perfect 2!")
        print("")
    else:
        if dollar < 2.00:
            print(" \n Oops! You were a little under 2!")
            print("")
            print(" Your total was: ", dollar)
        else:
            if dollar > 2.00:
                print(" \n Oh no! You went over 2!")
                print("")
                print(" Your total was: ",dollar)

main()
4

2 に答える 2

0

スタイルの問題として、すべてのコインをリストに入れてみませんか。

coin_list = [five, ten, twenty, fifty, one_dollar, two_dollar]
show_change(coin_list)
calculate_value(coin_list)
CalculateAndPrint(coin_list)

def注:上記の関数のを変更する必要があります。

于 2013-08-20T10:28:32.747 に答える
0

実際には、いくつかのエラーがあります。

関数 calculate_value は値を返しますが、まったく割り当てられていません

return_dollar = calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar)

その値を CalculateAndPrint に渡す必要があります。

CalculateAndPrint(return_dollar)

CalculateAndPrint の定義も変更する必要があります。

def CalculateAndPrint(dollar):

この PC には python 3.0 がインストールされていないため、すべてのプログラムをテストすることはできませんが、現在確認できる 2 つの問題があります。

于 2013-08-20T10:32:01.543 に答える