0

The script runs up until the 'takenotes' function is called and then just stops when it should run the function. There isn't any errors it just stops. Why is this?

# Please note that this only works in integer values, since there is no change in pence
notes = (1,5,10,20,50) #Value of notes
quantities = [10,8,5,5,1] #Quantities of notes
# Defining variables
notesout = []
total = 0
x = -1
payment = []
# This loop works out the total amount of cash in the cash register
while (x < 4):
        x += 1
        calc = notes[x]*quantities[x]
        total += calc
mon_nd = 70 # Money needed
def takenotes():
        print("Please input each notes value, when finished type \"stop\"")
        # If input is an int then add to payment list, if not then work out the change
        payment = [20,20,20,20]
        main()

def main():
        # Finds the value of the cash given
        paymentV = sum(payment)
        changeT = paymentV - mon_nd
        # Change the quantities of the 'quantities' variable
        for i in payment:
                quantities[notes.index(i)] = quantities[notes.index(i)] + 1
        while(changeT < 0):
                # Works out what amount of change should be given
                for i in reversed(notes):
                        if (changeT - i >= 0):
                                notesout.append(i)
                                quantities[notes.index(i)] = quantities[notes.index(i)]-1
                                changeT -= i
                        else:
                                return True
        print(notesout)
takenotes()
  • `payment = [20, 20, 20, 20]` does not modify the global variable, use `global` or better pass the value to the function `main()`. – bereal Oct 03 '15 at 14:29
  • 1
    actually @imaluengo that's wrong. Code Review is a site for **working** code, not for failing code. This means: Code must not only run, it must produce correct results, as such this question is off-ropic for [codereview.se]. For more information, please see: https://meta.stackoverflow.com/questions/253975/be-careful-when-recommending-code-review-to-askers?s=1|1.0000 – Vogel612 Oct 03 '15 at 14:33
  • @Vogel612 Ouch! Didn't know about that, sorry. Completely thought `CodeReview` actually was to review code and find bugs (as your linked question states, I'm one of those who didn't read CodeReview's help, but I just learnt a good lesson). Thanks! I will think twice before recommending codereview again! :P – Imanol Luengo Oct 03 '15 at 14:36

3 Answers3

0

It doesn't "just stop". takenotes calls main; it gets to the for loop inside the while loop; the first time round, changeT - i is not greater than 0, so it returns True. Since you do not do anything with the return value from main, nothing is printed, and the program ends.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

First of all, you need a global statement to change any global variable (like payment).

payment = []

def takenotes():
    global payment
    payment = [20, 20, 20, 20]

You also have no input() function in your code. See the docs.

David
  • 624
  • 6
  • 15
0

This script run correctly. It call the takenotes() function and then execute it normally (display message, set local payment array and then execute main() function). You could check this on this online Python interpreter. Also you could execute it step by step here to see what your script exactly do.

Also when you want to edit a global variable you must use the global statement. Read answer for this SO question for more info.

Community
  • 1
  • 1
Grzegorz Piotrowski
  • 913
  • 1
  • 10
  • 12