需要帮助理解为什么我的代码不运行[python]

时间:2016-06-29 11:52:09

标签: python-3.x

所以我的目标是为咖啡馆提供各种咖啡的订购系统。现在我无法理解为什么我的代码没有运行,每当我执行代码时没有任何反应,我不知道为什么

class Coffee(object):

    def __init__(self,name):
        self.name = name

    def coffee (self, coffee_type, price):
        self.coffee_type = coffee_type
        self.price = price

class Order(object):

    def __init__(self, name):
        self.name = name

    def newOrder (self, coffee_type, coffee_amount):
        this.coffee_type = coffee_type
        this.coffee_amount = coffee_amount




class Main(object):

    def __init__ (self, name):
        self.name = name


    from Coffee import coffee

    flat_white = coffee("1 : Flat White", 3.50)
    long_black = coffee("2 : Long Black", 3.50)
    cappuccino = coffee("3 : Cappuccino", 4.00)
    espresso = coffee("4 : Espresso", 3.25)
    latte = coffee("5 : Latte", 3.50)

    Ordering = 'Y'

    while Ordering == 'Y':
        try:
            order_coffee = int(input("What coffee would you like to order?", pprint (vars(coffee))))
            break

        except:
            ValueError
            print ("wrong input")

        order_amount = input("How many would you like to order?")

        if order_coffee == 1:
            new_Order(flat_white, order_amount)

        elif order_coffee == 2:
            new_Order(long_black, order_amount)

        elif order_coffee == 3:
            new_Order(cappuccino, order_amount)

        elif order_coffee == 4:
            new_Order(espresso, order_amount)

        elif order_coffee == 5:
            new_Order(latte, order_amount)

        else:
            print ("Please enter a valid number")
            break

1 个答案:

答案 0 :(得分:0)

您的代码看起来有几个问题。我会尽力帮助你和一对夫妇。

首先,不需要__init__方法来接受名称。只需让方法接受对象的必要信息:

class Coffee(object):
    def __init__ (self, coffee_type, price):
        self.coffee_type = coffee_type
        self.price = price

class Order(object):
    def __init__(self, coffee_type, coffee_amount):
        self.coffee_type = coffee_type
        self.coffee_amount = coffee_amount

其次,因为它实际上没有做任何事情,你可以将Main类替换为条件中的执行,如Python程序/脚本中常见的那样:

if __name__ =="__main__":
    ... # put main code here

接下来,编写的except将捕获所有异常,使调试更加困难。如果您只想抓住ValueError,请执行:

    except ValueError:
        print ("wrong input")

如果您使用当前流(即使用try except块,您应该将所有订单逻辑放在try块中,因为它在外面没有任何意义。此外,如果您有order_coffee,则无法定义所需的必要变量(ValueError

在接受订单输入后添加break将导致您在输入订单后中断订购循环。这可能不是预期的行为。这里不需要break。输出"Please enter a valid number"之后同样适用。

创建Order时,您只需拨打Order(coffee_type, coffee_amount)即可。创建此订单时,请确保将其设置为变量。可能是这样的:

new_order = Order(flat_white, order_amount)

打印“请输入有效数字”,“错误输入”略有冗余。您只需要其中一个在无效输入下打印。

最后,pprint会打印出来然后返回None,这样print(..., pprint(x))会在完成相当打印None之后打印额外的x,而只是拨打pprint,就像是对print的另一次通话一样。

- -

进行所有这些更改后,您将得到类似的结果:

class Coffee(object):

    def __init__ (self, coffee_type, price):
        self.coffee_type = coffee_type
        self.price = price

class Order(object):

    def __init__(self, coffee_type, coffee_amount):
        self.coffee_type = coffee_type
        self.coffee_amount = coffee_amount

if __name__ =="__main__":

    coffee_available=[Coffee("1 : Flat White", 3.50), 
                      Coffee("2 : Long Black", 3.50), 
                      Coffee("3 : Cappuccino", 4.00), 
                      Coffee("4 : Espresso", 3.25), 
                      Coffee("5 : Latte", 3.50)]

    ordering = 'Y'

    while ordering == 'Y':

        print("Coffee Type\t\tPrice")
        print("-----------\t\t-----")
        for coffee in coffee_available:
            print("{}\t- - -\t$ {}".format(coffee.coffee_type,coffee.price))
        print()
        order_coffee = int(input("What is the number of the coffee you want? "))

        order_amount = input("How many would you like to order? ")

        new_order = None

        if order_coffee >= 1 and order_coffee <=5:
            new_order = Order(coffee_available[order_coffee-1], order_amount)

        else:
            print ("Please enter a valid number")