Skip to content Skip to sidebar Skip to footer

Valueerror: Invalid Literal For Int() With Base 10 Cannot Figure Out Why

I cannot seem to figure out why when i run my program i receive the error ValueError: invalid literal for int() with base 10: 'Enter pennies : '. The entire program was made by my

Solution 1:

change this:

int(input("Enter Pennies: "))

to this:

pennies = input("Enter Pennies: ")

Edited I believe this is just a typo, you should be assigning pennies to the input result.

Solution 2:

def main():
    pennies = get_input1("Enter pennies : ")
    nickels = get_input2("Enter nickels : ")
    dimes = get_input3("Enter dimes : ")
    quarters = get_input4("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 = get_dollars(pennies, nickels, dimes, quarters)
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)

    print("Total = $", total_value, sep="")
    print("You have", dollars, "dollars and", left_over_cents, "cent(s)")

def get_input1(pennies):
    pennies = int(input("Enter Pennies: "))
    if int(pennies) < 0:
        print('Error: money cannot be negative')
        pennies = int(input('Enter correct amount of pennies: '))

    return(pennies)

def get_input2(nickels):
    nickels = int(input("Enter nickels: "))
    if int(nickels) < 0:
        print('Error: money cannot be negative')
        pennies = int(input('Enter correct amount of nickels: '))

    return(nickels)

def get_input3(dimes):
    dimes = int(input("Enter dimes: "))
    if int(dimes) < 0:
        print('Error: money cannot be negative')
        pennies = int(input('Enter correct amount of dimes: '))

    return(dimes)

def get_input4(quarters):
    quarters = int(input("Enter quarters: "))
    if int(quarters) < 0:
        print('Error: money cannot be negative')
        pennies = int(input('Enter correct amount of quarters: '))

    return(quarters)

main()

This will get you through defining your pennies,nickels,dimes and quarters. Now you will have to define get_total, get_dollar, and get_left_over_cents.

You needed to have pennies in front of the int value is why you were receiving the error. Then you can return that value of pennies to the main.

Hope this helps!

Solution 3:

Your data flow is backwards. In main(), you're calling get_input1 to retrieve values, much as with input(), but if we look at that get_input1 does:

# Names its argument, which was a question, "pennies"defget_input1(pennies):
    # Asks a question, converts to int, and discards itint(input("Enter Pennies: "))
    ifint(pennies) < 0:    # Converts pennies to int; but it was a question!print('Error: money cannot be negative')
        # This is the only place to alter pennies, but it doesn't return it
        pennies = int(input('Enter correct amount of pennies: '))

It looks like this used to be code that was refactored to handle the various coins with a function call each, but it doesn't have the two modifications it needs: a way to alter what it asks for, and a way to return what the answer was. Note that function arguments, like pennies in get_input1, are local variables; main never sees it changing.

Post a Comment for "Valueerror: Invalid Literal For Int() With Base 10 Cannot Figure Out Why"