Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting question
#1
Hey guys so I am just stuck on the formatting side of my problem: I have written the code correctly I just need to know a few key points please such as printing a list line by line for each item and also having a dollar sign next to the outputted results
My code asks the user to input a cash amount and then prints the change similar to what an atm would do

amount=int(input("Please enter amount"))
Notes = [100, 50, 20, 10, 5, 2, 1]
Returned = []
for i in Notes:
  while amount >=i:
        Returned.append(i)
        amount = amount - i
print ("Change Given ",Returned,sep='\n'"")
example output

Please enter amount47
Change Given 
[20, 20, 5, 2]
And my output actually needs to print this line by line not all as one line and how would I put a dollar sign in front of each item? Much appreciated in advance cheers :)
Reply
#2
print('Change given:')
for amt in Returned:
   print('{}'.format(amt))
Reply
#3
(Apr-28-2017, 10:51 PM)Larz60+ Wrote:
print('Change given:')
for amt in Returned:
   print('{}'.format(amt))

Ahh got it now thanks, I was trying to use the .join function but I kept getting an error
    print ('\n'.join(Returned))

TypeError: sequence item 0: expected str instance, int found

Oh and I am guessing to get the dollar sign it would be as simple as

print('Change given:')
for amt in Returned:
   print("$"'{}'.format(amt))
Reply
#4
you got it
Reply
#5
Just one last thing bud, I noticed in the instructions (not too sure if it's a big issue or not) that our output needs to have the initial amount shown before the change given amount is outputted

I just want to know why when I call the function of the user inputted amount it returns as a 0

e.g

amount=int(input("Please enter amount"))
Notes = [100, 50, 20, 10, 5, 2, 1]
Returned = []
for i in Notes:
 while amount >=i:
       Returned.append(i)
       amount = amount - i
print ("Initial amount ($): ",amount,"")
print('Change given:')
for amt in Returned:
  print("$"'{}'.format(amt))
and the output

Please enter amount47
Initial amount ($):  0 
Change given:
$20
$20
$5
$2
Thanks again!
Reply
#6
Print out amount prior to decrementing
also you don't need returned (unless specified in your assignment)
It's better not to use single letter names like i
amount = int(input("Please enter amount: "))
bal = amount
notes = [100, 50, 20, 10, 5, 2, 1]
print("Initial amount ($): {}".format(amount))
print('Change given:')
for amt in notes:
   while amount >= amt:
       print('${}'.format(amt))
       amount -= amt
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting question regarding tables (Interest loan calculator) Liquid_Ocelot 14 10,239 May-07-2017, 05:48 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020