简体   繁体   中英

Why does my code keep asking me for quarters 4 times?

I have this homework assignment and I'm not suppose to change this part of the code

def main():

    pennies = get_input("Enter pennies : ")
    nickels = get_input("Enter nickels : ")
    dimes = get_input("Enter dimes : ")
    quarters = get_input("Enter quarters : ")

    print("You entered : ")
    print("\tPennies  : " , pennies)
    print("\tNickels  : " , nickels)
    print("\tDimes    : " , dimes)
    print("\tQuarters : " , quarters)

    total_value = get_total(pennies, nickels, dimes, quarters)
    dollars, cents = get_left_over_cents(pennies, nickels, dimes, quarters)
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
    print("Total = $", format(total_value,".02f"), sep="")
    print('You have', dollars, "dollars and", cents, "cent(s)")

This is my code for the assignment that I've done

def main():
    pennies = get_input("Enter pennies : ")
    nickels = get_input("Enter nickels : ")
    dimes = get_input("Enter dimes : ")
    quarters = get_input("Enter quarters : ")

    print("You entered : ")
    print("\tPennies  : " , pennies)
    print("\tNickels  : " , nickels)
    print("\tDimes    : " , dimes)
    print("\tQuarters : " , quarters)

    total_value = get_total(pennies, nickels, dimes, quarters)
    dollars, cents = get_left_over_cents(pennies, nickels, dimes, quarters)
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
    print("Total = $", format(total_value,".02f"), sep="")
    print('You have', dollars, "dollars and", cents, "cent(s)")

def get_input(pennies):
    pennies = input("Enter Pennies: ")
    while int(pennies) < 0:
        print("Error: money cannot be negative")
        pennies = int(input("Enter correct amount of pennies: "))
    return pennies

def get_input(nickels):
    nickels = input("Enter nickels: ")
    while int(nickels) < 0:
        print("Error: money cannot be negative")
        nickels = int(input("Enter correct amount of nickels: "))
    return nickels

def get_input(dimes):
    dimes = input("Enter dimes: ")
    while int(dimes) < 0:
        print("Error: money cannot be negative")
        dimes = int(input("Enter correct amount of dimes: "))
    return dimes

def get_input(quarters):
    quarters = input("Enter quarters: ")
    while int(quarters) < 0:
        print("Error: money cannot be negative")
        quarters = int(input("Enter correct amount of quarters: "))
    return quarters

def get_total(pennies, nickels, dimes, quarters):
    amount_pennies = (int(pennies) * .01)
    amount_nickels = (int(nickels) * .05)
    amount_dimes = (int(dimes) * .10)
    amount_quarters = (int(quarters) * .25)
    return amount_pennies + amount_nickels + amount_dimes + amount_quarters

def get_left_over_cents(pennies, nickels, dimes, quarters):
    total = int(pennies) + 5*int(nickels) + 10*int(dimes) + 25*int(quarters)
    return total // 100, total % 100

main()

But everytime I run the code, it tells me to enter quarters 4 times. But I want it to say enter pennies, enter nickels, and so forth. Can someone help me? I'm really confused on how to make it do that without changing that core code

You keep redefining your get_input function. Each time you do so, the old get_input function is overshadowed and becomes no longer available. So, in the end, you only have the get_input you made for quarters.

You need to rename your functions so that each has a unique name that will not overshadow another function. Why not do something like:

def get_pennies(...):
    ...

def get_nickels(...):
    ...

def get_dimes(...):
    ...

def get_quarters(...):
    ...

Or, even better, make a single function that can handle all coin types. A (basic) example is below:

def get_coin(coin_type):
    amount = int(input("Enter " + coin_type + ": "))  # BTW, you forgot to call int() up here before.
    while amount < 0:
        print("Error: money cannot be negative")
        amount = int(input("Enter correct amount of " + coin_type + ": "))
    return amount

You would then call this function as:

pennies = get_coin('pennies')
nickels = get_coin('nickels')
...

As iCodez explains , every time you write def get_input(dimes): or def get_input(quarters): , you're just creating a new definition for the function get_input , and hiding the old one. The fact that the parameter has a different name doesn't mean anything. (How could it? When you write pennies = get_input("Enter pennies : ") , how would Python know that you want to call get_input(pennies) instead of get_input(quarters) ?)

You can fix this by giving each one a unique name ( get_pennies , get_nickels , etc.), and calling them by the unique names.

Also, notice that you're not actually using the argument pennies or nickels or whatever. You're taking a prompt string, and doing nothing with it.

And that's important, because it means you can refactor them all into one function. If you look at the implementations you've written, they're already almost identical; the only difference is in the prompt strings you print out. And you're already getting a different prompt string with each call. So, why not just use it?

def get_input(prompt):
    number = input(prompt)
    while int(number) < 0:
        print("Error: money cannot be negative")
        number = int(input(prompt))
    return number

Now, pennies = get_input("Enter pennies : ") will ask you for pennies, nickels = get_input("Enter nickels : ") will ask you for nickels, etc.


As a side note, there's a serious bug in all four of your implementations (and in my merged version(). If your first input is non-negative, the function will return the string, like "3" ; if that one is negative, but a later one is not, the function will return the integer value, like 5 . You probably want to make the first number = … and the second one identical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM