2017-02-11 28 views
0

我想打印的總購物清單,但每次我呼籲字符串時它打印0,而不是它應該是什麼。str未被要求時重置爲0?

cash_due = 0 
import pickle 
picklee = open('Store_stuff.pickle', 'rb') 
contents = pickle.load(picklee) 
picklee.close() 


shopping_list = ['Name  price  quantity  total'] 

store_contents ='''Store Contents 

Name  Price  GTIN-8 Code 
Butter  £1.20  70925647 
Chocolate £1.00  11826975 
Bread  £1.00  59217367 
Cheese  £2.80  98512508 
Bacon  £2.40  92647640 
Lamb  £4.80  49811230 
Ham   £2.59  53261496 
Potatoes £2.00  11356288 
Chicken  £3.40  89847268 
Eggs  £1.29  21271243''' 

def item(barcode, quantity, cash_due, shopping_list): 
    shopping_list.append(contents[barcode]['name']+'  £'+(str((int(contents[barcode]['price']))/100))+'   '+str(quantity)+'   £'+str((int(quantity)*int(contents[barcode]['price']))/100)) 
    print(cash_due) 
    print(contents[barcode]['price']) 
    print(quantity) 
    cash_due += ((int(contents[barcode]['price'])*(int(quantity)))/100) 
    print(cash_due) 

def shopkeeper_ui(): 
    print('Welcome to Stanmore\'s Food Emporium! Feel free to browse.') 
    print(store_contents) 
    user_input = '' 

    while user_input != 'finish': 
     user_input = input('''Welcome to the checkout. 
instructions - 
if you are entering text make sure your \'CAP\'s Lock\' is turned off 
if you are entering a barcode number, please enter it carefully 
if you want to print your current recipt, enter \'recipt\' 
if you want to see your current total, enter \'total\' 
and if you are finished, enter \'finish\' 

You can see the stores contents below 
Thanks for shopping: ''') 

     if len(user_input) == 8: 
      quantity = int(input('Enter the quantity that you want: ')) 
      item(user_input, quantity, cash_due, shopping_list) 
     elif user_input == 'recipt': 
      count8 = 0 
      for i in shopping_list: 
       print(shopping_list[count8]) 
       count8 += 1 
     elif user_input == 'finish': 
      print('Your shopping list is',shopping_list,' \nand your total was', total,'\n Thank you for shopping with Stanmore\'s Food Emporium') 
     elif user_input == 'total': 
      print('your total is, £',cash_due) 
     else: 
      print('User_input not valid. Try again...') 
shopkeeper_ui() 

如果我輸入密碼,我的第一項是21271243(條形碼雞蛋)。然後我輸入4的數量。我可以得到shopping_listlist瞭解總,如果我從它理解它,但只要我嘗試從shopkeeper_ui函數中調用cash_due它打印0,而不是應該是什麼5.12項功能中打印字符串cash_due?

+1

cash_due不可變。在離開功能時,項目功能的改變會丟失。 –

+0

確定,所以你知道一種方法,使變化留出的功能 – Lomore

回答

2

cash_due is not mutable。離開功能時,item功能中的更改將丟失。

一般來說,解決方法是讓函數(item)返回值。

在這種情況下,我只想保持cash_dueitem功能,讓item只返回的費用用於該項目。事情是這樣的:

def item(barcode, quantity, shopping_list): 
    shopping_list.append(contents[barcode]['name']+'  £'+(str((int(contents[barcode]['price']))/100))+'   '+str(quantity)+'   £'+str((int(quantity)*int(contents[barcode]['price']))/100)) 
    print(contents[barcode]['price']) 
    print(quantity) 
    cost = ((int(contents[barcode]['price'])*(int(quantity)))/100) 
    print(cost) 
    return cost 

[...] 

     if len(user_input) == 8: 
      quantity = int(input('Enter the quantity that you want: ')) 
      cash_due += item(user_input, quantity, shopping_list) 

你不必與在和shopping_list同樣的問題,因爲它是一個可變的:它代替改變。閱讀關於mutables來理解這個概念。

但是,它可能是更好的設計,不讓項修改列表。它可能只是返回列表元素和成本,並且調用者會修改列表。

def item(barcode, quantity): 
    stuff = (contents[barcode]['name']+'  £'+(str((int(contents[barcode]['price']))/100))+'   '+str(quantity)+'   £'+str((int(quantity)*int(contents[barcode]['price']))/100)) 
    cost = ((int(contents[barcode]['price'])*(int(quantity)))/100) 
    return stuff, cost 

[...] 

     if len(user_input) == 8: 
      quantity = int(input('Enter the quantity that you want: ')) 
      stuff, cost = item(user_input, quantity, shopping_list) 
      shopping_list.append(stuff) 
      cash_due += cost