0

Code explanation

Here is the code I have so far.

value=float(input('Enter a number: '))
value2=float(input('Enter a number: '))    
value3=value+value2    
value4=float(input('Enter a number: '))    
value5=value3-value4

class sweetCurrency:   
    def  __init__(self, value):   
        self.__value = value

    def convertToList(self, value):   
        cList=[0,1,2,3,4,5,6,7,8,9]    
        cList[0] = value//10000
        cList[1] = (value-cList[0]*10000)//5000    
        cList[2] = (value-cList[0]*10000-cList[1]*5000)//2000    
        cList[3] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000)//1000    
        cList[4] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000)//500   
        cList[5] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500)//100    
        cList[6] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100)//25    
        cList[7] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25)//10   
        cList[8] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25-cList[7]*10)//5   
        cList[9] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25-cList[7]*10-cList[8]*5)

        print(cList)

    def __str__(self):    
        print('There are',cList[0],'hundred dollar bills',cList[1],'fifty dollar bills',cList[2],'twenty dollar bills',    
              cList[3],'ten dollar bills',cList[4],'five dollar bills',cList[5],'one dollar bills',cList[6],'quarters',   
              cList[7],'dimes',cList[8],'nickels',cList[9],'pennies')

def main():    
    FirstVal=sweetCurrency(value)    
    FirstVal.convertToList(value)   
    print(FirstVal)

    SecondVal=sweetCurrency(value3)   
    SecondVal.convertToList(value3)   
    print(SecondVal)

    ThirdVal=sweetCurrency(value5)    
    ThirdVal.convertToList(value5)

    if value4>value3:   
        print('Error: cannot be higher than last value')

    else:   
        print(ThirdVal)

main()

When I run the code I get NameError: cList is not defined. Sorry for any formatting issues this is my first time using this site.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 3
    use `self.cList` – sahasrara62 Nov 01 '20 at 09:16
  • 1
    Does this answer your question? [Python class NameError. Var is not defined](https://stackoverflow.com/questions/43478782/python-class-nameerror-var-is-not-defined) or even better: https://stackoverflow.com/questions/50675248/why-am-i-getting-a-nameerror-when-i-try-to-access-an-attribute-in-my-class – Tomerikoo Nov 01 '20 at 09:23

2 Answers2

0

You computed a cList in convertToList but did not return or save it,thus it is lost.

You should add a self.cList = cList to save the value and add a cList = self.cList to revive it.

If you use this approach, you should also add a self.cList=...some value... in __init__ or the code will break if you tried to print the class before calling convertToList.

BTW,the code can be written more campactly by using str.format or %d methods

Code:

...
def convertToList(self, value):

    cList=[0,1,2,3,4,5,6,7,8,9]

    cList[0] = value//10000

    cList[1] = (value-cList[0]*10000)//5000

    cList[2] = (value-cList[0]*10000-cList[1]*5000)//2000

    cList[3] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000)//1000

    cList[4] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000)//500

    cList[5] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500)//100 

    cList[6] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100)//25

    cList[7] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25)//10

    cList[8] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25-cList[7]*10)//5

    cList[9] = (value-cList[0]*10000-cList[1]*5000-cList[2]*2000-cList[3]*1000-cList[4]*500-cList[5]*100-cList[6]*25-cList[7]*10-cList[8]*5)

    self.cList=cList

def __str__(self):

    return 'There are %d hundred dollar bills %d fifty dollar bills %d twenty dollar bills %d ten dollar bills %d five dollar bills %d one dollar bills %d quarters %d dimes %d nickels %d pennies'%tuple(self.cList)
xkcdjerry
  • 965
  • 4
  • 15
0

There are two modifications you need to make to make this work the way you intend.

  1. Replace cList with self.cList
def convertToList(self, value):

        self.cList=[0,1,2,3,4,5,6,7,8,9]
        self.cList[0] = value//10000
        self.cList[1] = (value-self.cList[0]*10000)//5000
        self.cList[2] = (value-self.cList[0]*10000-self.cList[1]*5000)//2000
        self.cList[3] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000)//1000
        self.cList[4] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000)//500
        self.cList[5] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000-self.cList[4]*500)//100 
        self.cList[6] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000-self.cList[4]*500-self.cList[5]*100)//25
        self.cList[7] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000-self.cList[4]*500-self.cList[5]*100-self.cList[6]*25)//10
        self.cList[8] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000-self.cList[4]*500-self.cList[5]*100-self.cList[6]*25-self.cList[7]*10)//5
        self.cList[9] = (value-self.cList[0]*10000-self.cList[1]*5000-self.cList[2]*2000-self.cList[3]*1000-self.cList[4]*500-self.cList[5]*100-self.cList[6]*25-self.cList[7]*10-self.cList[8]*5)
        print(self.cList)

This is important to ensure that cList is retrievable later and it "connected" to the class instance. Currently what happens is: You call FirstVal.convertToList() and you assume that the cList is calculated and stored somewhere. But it actually is only calculated and stored within the scope of that method! Once you leave the scope of the convertToList() method, the cList is destroyed. To prevent that (assuming you need these values), you would need to reassign them to an attribute of the instance, i.e. to self.cList. These then become part of the FirstVal, SecondVal... instances and you can refer to them in other methods called after (e.g. in print(FirstVal)).

  1. Ensure that def __str__(self): returns and not just prints a string (and also rename all cList references to self.cList:

 def __str__(self):
        return f'There are {self.cList[0]} hundred dollar bills {self.cList[1]} fifty dollar bills' +\
               f'{self.cList[2]} twenty dollar bills {self.cList[3]} ten dollar bills {self.cList[4]}' +\
               f'five dollar bills {self.cList[5]} one dollar bills {self.cList[6]} quarters' +\
               f'{self.cList[7]} dimes {self.cList[8]} nickels {self.cList[9]} pennies'

(I added the f'' string formatting just as an example, but you could also use the + operator to construct a single string to return).

You can see more examples of how to use __str__ and __repr__ on a class here.

tania
  • 2,104
  • 10
  • 18