Python help coin change

Can someone please help me on this code.

print (‘Total Change’)

TC1=(int(input('How many pennies do you have: ')))

q=(int(input(‘You have ’ +str(TC1//25)+ ’ quarters’)))

d=(int(input(’, ’ +str((TC1-(q*25))//10+ ’ dimes’)))

n=(int(input(’, ’ +str((TC1-(q25)-(d10))//5)+ ’ nickels’)))

p=(int(input(’,and ’ +str((TC1-(q25)-(d10)-(n*5))//1)+ ’ pennies.’)))

ValueError: invalid literal for int() with base 10: ‘’

First: when posting multiple lines of code on CD, you can put ``` before and after the whole thing, and it will render nicely, and be easier for other people to copy and paste.

Second: I assume that you get this error when the program prints You have $Q quarters, and you hit the ENTER key. Is that right?

1 Like

The program does the correct amount of quarters and then will post the ValueError message I posted at the bottom.

For generic programming you can type your errors into google verbatim and often find the solution to your problem. You might need to click a few times and do some critical thinking, but it will be faster in the long run and it’s a good skill to develop.

10 Likes

Good, that’s what I’d expect from this code.

The first two lines look alright to me:

print ('Total Change')
TC1=(int(input('How many pennies do you have: ')))

Two minor nitpicks:

  1. I believe it would make more sense if the prompt was:

How many cents do you have?

  1. You don’t need the parentheses around the whole input-getting expression. This works fine:

TC1=int(input('How many cents do you have: '))

But like I said, those are minor nitpicks. The error is on this line:

q=(int(input('You have ' +str(TC1//25)+ ' quarters')))

Clearly, this line doesn’t do what you expect it to do. Just to make sure everything is straight: when you wrote this line, what did you expect it to do, @curtissmith003 ?

1 Like

If your code is displaying the number of quarters before giving you the error message, the error is likely coming from that line.

q=(int(input(‘You have ’ +str(TC1//25)+ ’ quarters’)))

What it’s doing:

  1. input() - Display the message within the parentheses and collect the keyboard input from the console
  2. int() - Convert the keyboard input into an integer
  3. q= - Store the converted integer as the variable q

If you just hit enter after the message displays, the value from the input() function will be an empty string ''. When this gets passed to the int() function, it doesn’t know what to do with it, so it throws the error you’re seeing. You’ll get the same error if you just type the command int('') into a Python console.

You are incorrectly using “input()” in most statements. input() is for receiving typed input from the user. Most of your statements should be “print()”. After the input, you should first compute the change and then print them.

Nit picks:

  • why wrap every assignment in an outer paren ("()"). Total not needed
  • you are already calculating integers, so no need for most of those int() calls.

So something like:

tc1 = int(input('How many pennies:'))
q = tc1 // 25
d = (tc1 - 25*q) // 10
...
print(f'Total change: you have {q} quarters, {d} dimes...')

Note I have used the “f-string” for formatting, which is a recently added feature in 3.6 and on. Using “+ str() +” is not the recommended method. See for example https://realpython.com/python-f-strings/