如果Python中的语句跳过

时间:2018-04-22 11:15:45

标签: python python-3.x if-statement nested

我在我的代码行中创建了嵌套条件,但是如果任何其他“(书籍)条件”条件不“好”,我想跳过它们。

condition = input("What is the condition of the book? ")
age = input("Is the book old or new? ")
cover = input("What type of cover does it have? ")


if condition == "good":
    if age == "new":
        if cover == "hardcover":
            print("We can offer you a high price")
        elif cover == "papercover":
            print("We can offer you a medium price")
    elif age == "old":
        print("We can offer you a medium price")
elif condition == "poor":
    print("We can offer a low price")
elif condition == "terrible":
    print("Sorry we cannot accept this book")
else:
    print("I do not understand, please leave the store")

1 个答案:

答案 0 :(得分:0)

您需要将输入移动到if语句中,这样就可以使用已经存在的if-else结构,如下所示:

condition = input("What is the condition of the book? ")

if condition == "good":
    age = input("Is the book old or new? ")
    if age == "new":
        cover = input("What type of cover does it have? ")
        if cover == "hardcover":
            print("We can offer you a high price")
        elif cover == "papercover":
            print("We can offer you a medium price")
    elif age == "old":
        print("We can offer you a medium price")
elif condition == "poor":
    print("We can offer a low price")
elif condition == "terrible":
    print("Sorry we cannot accept this book")
else:
    print("I do not understand, please leave the store")