为什么打印“ else”

时间:2019-08-11 19:59:51

标签: python

好的,所以这段代码可以正确运行,但是在我输入应该给我“你拿了几个黑莓”的输入之后,它会打印“ else”之后应该打印的内容。

if move == "up":
    forest()
    move = input("To the east there is a wooden cabin. ")
    if move == "pick blackberries":
        print("You pick a handful of blackberries.")
    if move == "take blackberries":
        print("You take a handful of blackberries")
    if move == "take blackberry branch":
        print("You scrape yourself badly on the blackberry brambles.")
    if (move == "s" or move == "south" or move == "w" or move == "west"):
        print("The blackberry bushes are impenetrable.  You scrape yourself quite badly.")
    else:
        print("I don't understand that.")

其运行方式如下:

PS C:\Users\Olga\lpthw> python functions.py
Darkness surrounds you.  You are likely to be eaten by a grue.
Above you, you see some light breaking through the rock ceiling. up
You climb out of the cave.  You are in a dense forest.
Below you is the hole in the ground you climbed out of.
To the north you see a footpath.
To the south and west there is a dense growth of trees and blackberry bushes.
To the east there is a wooden cabin. take blackberries
You take a handful of blackberries
I don't understand that.

即使我没有随机输入内容,它也会自动打印“我不明白”和“你带了几个黑莓”。

如果我运行它并输入“ Blah”,它会正确打印“我听不懂”。

此外,如果我摆脱了除“服用黑莓”和其他使我们“不了解”的else语句之外的所有内容,那么我只有此代码...

if move == "up":
    forest()
    move = input("To the east there is a wooden cabin. ")
    if move == "take blackberries":
        print("You take a handful of blackberries")
    else:
        print("I don't understand that.")

实际上很好,并且不会显示“我不理解”。

所以我猜这里的代码有问题:

if move == "pick blackberries":
        print("You pick a handful of blackberries.")
    if move == "eat blackberries":
        print("They are quite tasty.")

    if move == "take blackberries":
        print("You take a handful of blackberries")
        #inventory.append("blackberries")
    if move == "take blackberry branch":
        print("You scrape yourself badly on the blackberry brambles.")
    if (move == "s" or move == "south" or move == "w" or move == "west"):
        print("The blackberry bushes are impenetrable.  You scrape yourself quite badly.")

是什么把它扔掉?

谢谢!

2 个答案:

答案 0 :(得分:0)

您制作了很多If语句,但是else仅适用于最后一个。

要制作if语句的“块”,请像这样使用elif:

if move == "up":
    forest()
    move = input("To the east there is a wooden cabin. ")
    if move == "pick blackberries":
        print("You pick a handful of blackberries.")
    elif move == "take blackberries":
        print("You take a handful of blackberries")
    elif move == "take blackberry branch":
        print("You scrape yourself badly on the blackberry brambles.")
    elif (move == "s" or move == "south" or move == "w" or move == "west"):
        print("The blackberry bushes are impenetrable.  You scrape yourself quite badly.")
    else:
        print("I don't understand that."

那应该解决它。

答案 1 :(得分:0)

如果您具有多个属于在一起的if语句(因此,仅当前一个为false时才评估下一个),则应使用elif

因此,以if开始,然后是一个或多个elif(读为“ el(se)if:”),最后是else:(当然不是)。

输入您的代码

if move == "up":
    forest()
    move = input("To the east there is a wooden cabin. ")
    if move == "pick blackberries":
        print("You pick a handful of blackberries.")
    elif move == "take blackberries":
        print("You take a handful of blackberries")
    elif move == "take blackberry branch":
        print("You scrape yourself badly on the blackberry brambles.")
    elif (move == "s" or move == "south" or move == "w" or move == "west"):
        print("The blackberry bushes are impenetrable.  You scrape yourself quite badly.")
    else:
        print("I don't understand that.")