Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Issue
#1
So, this is interesting...

It prints Pam 205.36 2 times

what happened to Bruce 37.95?

class check:
    payee = ''
    amount = 0.0

c = check          #create a check object 
checkbook = [c]    #checkbook is a list of checks

c.payee = 'Bruce'
c.amount = 37.95
checkbook.append(c) #add check to checkbook

c.payee = 'Pam'
c.amount = 205.36
checkbook.append(c) #add check to checkbook


#print them
for each_check in range(1,len(checkbook)):
   print(check.payee,check.amount)
Reply
#2
class check:
    payee = ''
    amount = 0.0
 
checkbook = []    #checkbook is a list

c = check()
c.payee = 'Bruce'
c.amount = 37.95
checkbook.append(c) #add check to checkbook

c = check()
c.payee = 'Pam'
c.amount = 205.36
checkbook.append(c) #add check to checkbook
 
 
#print them
for check in checkbook:
   print(check.payee,check.amount)
Output:
Bruce 37.95 Pam 205.36
Reply
#3
You didn't make a new instance of check, you altered the first one and added it to the list.
Reply
#4
Thanks, got it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 506 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Python List Issue Aggie64 5 1,607 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  List to table issue robdineen 2 1,459 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,252 Jun-11-2021, 10:13 PM
Last Post: topfox
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,205 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  For List Loop Issue Galdain 2 2,045 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,521 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,469 Sep-20-2019, 11:57 AM
Last Post: perfringo
  Basic List Issue rogueakula 1 2,178 May-18-2019, 06:01 PM
Last Post: snippsat
  List slicing issue Irhcsa 3 2,970 Apr-26-2019, 09:16 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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