str 在不被要求时重置为 0?

str reset to 0 when not asked to?

我正在尝试打印购物清单的总数,但每次我调用该字符串时,它都会打印 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_list list 来了解总数,如果我从项目函数内部打印字符串 cash_due 它会理解它但是一旦我尝试调用 cash_due从 shopkeeper_ui 函数它打印 0 而不是 5.12?

cash_due is not mutableitem 函数中的更改在离开函数时丢失。

一般来说,解决这个问题的方法是让函数 (item) return 的值。

在这种情况下,我只会将 cash_due 排除在 item 函数之外,而让 item 仅 return 该项目的成本。像这样:

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 没有相同的问题,因为它是可变的:它已就地更改。阅读可变变量以了解概念。

不过,最好不要让项目修改列表。它可以 return 列表元素和成本,调用者将修改列表。

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