Python脚本的某些部分未运行

时间:2015-03-28 21:54:33

标签: python-3.x

在选择选项2部分下。如果输入是面包,则该部分的其余部分将不会运行。脚本就在那里结束。有人告诉我有什么问题吗?我没有收到脚本停止的任何错误消息。除此之外一切正常

bread = 44
lettuce = 21
meat = 21
cheese = 23
sandwich = bread, lettuce.meat, cheese

choseOption = input('What you like to mark an order, add to stock, or check stock')

if choseOption == 1:
    newOrder = input('what did they order?')
    if newOrder == 'sandwich':
        bread = bread - 4
        lettuce = lettuce - 5
        meat = meat - 7
        cheese = cheese - 10
        print(bread)
        print(lettuce)
        print(meat)
        print(cheese

if choseOption == 2:
    newStock = input('What would you like to add to stock?')
    if choseOption == 'bread':
        addBread = input('How much bread would you like to add? ')
        bread = bread + addBread

    if newStock == 'lettuce':
        addLetuce = input('How much lettuce would you like to add?')
        lettuce = lettuce + addLetuce

    if newStock == 'meat':
        addMeat = input('how much meat would you like to add?')
        meat = meat + addMeat

    if newStock == 'cheese':
        addCheese = input('how much cheese would you like to add?')
        cheese = cheese + addCheese

if choseOption == 3:
    viewItem = input('what item do you want to view?')

    if viewItem == 'bread':
        print(bread)

    if viewItem == 'lettuce':
        print(lettuce)

    if viewItem == 'meat':
        print(meat)

    if viewItem == 'cheese':
        print(cheese)

2 个答案:

答案 0 :(得分:1)

    print(cheese

需要结束)

答案 1 :(得分:0)

问题在于:

        print(cheese

if choseOption == 2:

由于print(cheese未关闭,choseOption == 2:被解释为print命令的一部分。建议代码:

bread = 44
lettuce = 21
meat = 21
cheese = 23
sandwich = bread, lettuce.meat, cheese

choseOption = input('What you like to mark an order, add to stock, or check stock')

if choseOption == 1:
    newOrder = input('what did they order?')
    if newOrder == 'sandwich':
        bread = bread - 4
        lettuce = lettuce - 5
        meat = meat - 7
        cheese = cheese - 10
        print(bread)
        print(lettuce)
        print(meat)
        print(cheese)

if choseOption == 2:
    newStock = input('What would you like to add to stock?')
    if choseOption == 'bread':
        addBread = input('How much bread would you like to add? ')
        bread = bread + addBread

    if newStock == 'lettuce':
        addLetuce = input('How much lettuce would you like to add?')
        lettuce = lettuce + addLetuce

    if newStock == 'meat':
        addMeat = input('how much meat would you like to add?')
        meat = meat + addMeat

    if newStock == 'cheese':
        addCheese = input('how much cheese would you like to add?')
        cheese = cheese + addCheese

if choseOption == 3:
    viewItem = input('what item do you want to view?')

    if viewItem == 'bread':
        print(bread)

    if viewItem == 'lettuce':
        print(lettuce)

    if viewItem == 'meat':
        print(meat)

    if viewItem == 'cheese':
        print(cheese)