Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么要打印“else”_Python - Fatal编程技术网

Python 为什么要打印“else”

Python 为什么要打印“else”,python,Python,好的,这段代码运行时没有错误,但是它打印出了它只应该打印的内容,在我输入了应该给我的输入之后,你可以拿一把黑莓 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 == "t

好的,这段代码运行时没有错误,但是它打印出了它只应该打印的内容,在我输入了应该给我的输入之后,你可以拿一把黑莓

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,它会正确打印,但我不明白这一点

另外,如果我去掉了所有的东西,除了takeblackberries和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.")
扔掉它是什么


谢谢

你做了很多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."

这应该可以解决问题。

如果有多个If语句属于同一个语句,那么只有在前一个语句为false时,下一个语句才会被计算,那么应该使用elif

因此,从一个if开始,然后一个或多个elif读取else if:最后yelse:当然不是

对于您的代码:

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.")

您可以像if移入['s'、'south'、'w'、'west']那样简单地执行此操作:else与最后一个if语句相连接,而不是与前面行中的所有其他if语句相连接。你可能想用elif,当然!我对编码很陌生,谢谢你给我这个提示