Question

I'm doing a Python programming course and I've been trying to wrap my head around how I can make it happen. I've written some code and I'm trying to fix any errors that pop up but I'm getting confused and not solving anything which is why I turn to you guys. I would appreciate any ideas and suggestions that pop up when you see what I've managed to write so far. I'm especially having trouble figuring out how to do the last part where I have to get the value that is above or below $2.

I'm doing this exercise:

Create a change-counting game that gets the user to enter the number of coins necessary to make exactly two dollars. Implement a Python program that prompts the user to enter a number of 5c coins, 10c coins, 20c coins, 50c coins, $1 coins and $2 coins. If the total value of those coins entered is equal to two dollars, then the program should congratulate the user for winning the game. Otherwise the program should display a message advising that the total was NOT exactly two dollars, and showing how much the value was above or below two dollars.

Update: I made a few changes to the last function and it worked perfectly fine.

#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()
Était-ce utile?

La solution

well, actually there're a couple of errors:

the function calculate_value does return a value but it's not assigned at all

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

and you have to pass that value to CalculateAndPrint.

CalculateAndPrint(return_dollar)

you have also to change the definition of CalculateAndPrint:

def CalculateAndPrint(dollar):

i don't have python 3.0 installed on this PC so i cannot test all your program, but those are two problem i can see right now.

Autres conseils

Just as a matter of style, why don't you put all your coins in a list:

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

Note: you'll need to change the def's of the above functions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top