noprob=["nothing","none","no damage"]
while True:
q1=input("input your answer:")
answer=q1.split(' ')`
if any(a in answer for a in noprob):
function()#wont call this
break
else:
print("else statement")
continue
def function():
print("code now works")
我的代码不会在应该的时候调用该函数。此代码应识别用户输入并输出必要的操作。
新的整个代码:添加了elif语句,我认为这是代码的问题
`noprob=["nothing","none","no damage"]
something=["something","yes"]`
def function():
print("code now works")`
def something():
print("something, code works")
while True:
q1=input("input your answer:")
answer=q1.split(' ')`
if any(a in answer for a in noprob):
function()#wont call this
break
elif any(a in answer for a in something):
something()
break
else:
print("else statement")
continue
答案 0 :(得分:2)
Python从上到下执行代码。因此,如果您之前没有声明其定义,那么您将无法执行function
。在while
循环之前移动函数。
noprob=["nothing","none","no damage"]
def function():
print("code now works")
while True:
q1=input("input your answer:")
answer=q1.split(' ')
if any(a in answer for a in noprob):
function()#wont call this
break
else:
print("else statement")
continue
答案 1 :(得分:0)
您的问题是,您的列表和功能都名为something
。重命名一个并相应地更改您的代码。
something=["something","yes"]
def something(): #this one shadows your list and needs to be renamed
print("something, code works")
对于生成器,Python 3.5