elif语句语法错误简单代码

elif语句语法错误简单代码

本文介绍了Python编程错误,elif语句语法错误简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
    print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")

elif choice==("maybe"):
    print("You found an easter egg, congrats. PS this does nothing")

这段代码怎么了?它在idle shell语法错误中说。最后一个elif语句不起作用。

Whats wrong with this code?? It says in the idle shell syntax error. The last elif statement isn't working.

推荐答案

这是一个小的缩进问题,你的<$ c $打印语句c>如果块没有缩进,那么 elif 似乎不合适。请注意,python通过缩进保持。 / p>

This is a petty indentation issue, your print statements for the if blocks are not indented right and so the elif seems to be out of place. Note that python keeps track of logical blocks by the indentation.

name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
    print("Ok", name,", listen up")
    print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
    choice2=input("What do you want to call the house?")
    print("The old,",choice2,"was once owned by an old lady. ")

elif choice==("maybe"):
    print("You found an easter egg, congrats. PS this does nothing")

如前所述,如果选择==(是或是或是或是 )错误,请使用 if choice.lower()。strip()==yes,或如果选择(是,是,是,是

As already pointed out, if choice==("yes" or "yes " or "Yes" or "Yes ") is wrong, use if choice.lower().strip() == "yes" instead, or if choice in ("yes", "yes ", "Yes", "Yes ").

如果这是python 2,输入将抛出错误,改为使用 raw_input
同样 print 如果像函数一样使用多个语句也会抛出错误,所以从 print(statement_x,statement_y,statement_z)更改它们) print statement_x,statement_y,statement_z

If in case this is python 2, input will throw an error, use raw_input instead.Also print with multiple statements will throw errors as well if used like a function, so change them from print(statement_x, statement_y, statement_z) to print statement_x, statement_y, statement_z

这篇关于Python编程错误,elif语句语法错误简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:37