简体   繁体   English

逻辑和调用函数。 帮我理解这段代码

[英]Logic and calling functions. Help me understand this code

I have some code that is basically a demo for how dict works. 我有一些代码基本上是dict如何工作的演示。 My problem is, I am not sure exactly when it is calling the process_order() function. 我的问题是,我不确定它何时调用process_order()函数。 It seems like the main loop ( go_shopping ) never calls it, but the script seems to work. 似乎主循环( go_shopping )从不调用它,但是脚本似乎可以工作。 The go_shopping function calls get_item , but that doesn't call process_order either. go_shopping函数调用get_item ,但是也不调用process_order I am also having trouble with the line if not process_order(order, cart): . if not process_order(order, cart):我也会遇到麻烦。 What does the if not part mean in this case? 在这种情况下, if not部分是什么意思? Is that where it is calling process_order ? 那是在哪里调用process_order吗? It doesn't seem like it from the print statements, other wise it should print 'if not' when you add an item to the cart dictionary object. 从打印语句中看起来似乎不是,否则,当您向购物车字典对象中添加项目时,它应该打印“ if not”。

Am I on the right track or missing something simple? 我是在正确的轨道上还是缺少简单的东西?

Code: 码:

#!/usr/bin/python3.5

def get_item():
    print("[command] [item] (command is 'a' to add, 'd' to delete, 'q' to quit.)")
    line = input()

    command = line[:1]
    item = line[2:]

    return command, item

def add_to_cart(item, cart):
    if not item in cart:
        cart[item] = 0
    cart[item] += 1

def delete_from_cart(item, cart):
    if item in cart:
        if cart[item] <= 0:
            del cart[item]
        else:
            cart[item] -= 1

def process_order(order, cart):
    command, item = order

    if command == "a":
        add_to_cart(item, cart)
        print('added to cart')
    elif command == "d" and item in cart:
        delete_from_cart(item, cart)
    elif command == "q":
        return False
        print ('end process_order func')
    return True

def go_shopping():
    cart = dict()

while True:
    print ('start main loop')
    order = get_item()
    print ('exited process_order')
        if not process_order(order, cart):
            print ('if not')
            break
    print ('while loop end')
    print (cart)
    print ("Finished!")

go_shopping()

Thing is, I am not really sure of your problem. 问题是,我不确定您的问题。 But you seem concerned with the moment when process_order method is called. 但是,您似乎担心何时调用process_order方法。

When you write 当你写

if not process_order(order, cart)

it must be seen as follows (just adding parentheses): 必须如下所示(仅添加括号):

if (not process_order(order, cart))

So you're asking Python to do something if the condition not process_order(order, cart) is true. 因此,您要让Python做一些not process_order(order, cart)的条件。 So, Python must know the boolean value of the expression not process_order(order, cart) . 因此,Python必须知道表达式的布尔值,而not process_order(order, cart)

This expression is composed of a unary operator, not , and a non-elementary expression, process_order(order, cart) . 此表达式由一元运算符not和非基本表达式process_order(order, cart) That latter expression needs to be evaluated, so Python has to run the process_order method. 需要评估后一个表达式,因此Python 必须运行process_order方法。

Therefore, when you write if not process_order(order, cart) , the process_order method is indeed executed. 因此,如果您编写的if not process_order(order, cart) ,则确实会执行process_order方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM