Python if和elif问题总是被无规则地跳过
代码如下:Python if和elif问题总是被无规则地跳过,python,Python,代码如下: L = '?' ignore_letters = ['!','?','.',','] print("type 'commands' for list of commands") name = input('hello what is your name :) ') def main(): global restart answer = input('please type your request here '+name+': ') def answer():
L = '?'
ignore_letters = ['!','?','.',',']
print("type 'commands' for list of commands")
name = input('hello what is your name :) ')
def main():
global restart
answer = input('please type your request here '+name+': ')
def answer():
global answer
if 'list' in [answer] and 'take' in [answer]:
print('ok')
i = input('type your list '+name+' ')
l = i.split()
global L
L = l.sort()
main()
elif 'run' in [answer] and 'list' in [answer]:
print('ok sure')
print('here ' + L )
main()
(此代码只是代码行的一部分,但我认为问题应该在这里)
我还尝试了答案周围有其他类型的括号,但没有,只是为了确保答案不是这样
我对编码有点陌生,所以我觉得我的代码有点问题,我看不出你在代码中错误地使用了
global
关键字
注:
全局关键字用于从非全局范围(例如函数内部)更改或创建全局变量。
以代码为例
def funcA():
varA = 20
def funcB():
global varA
varA = 25
funcB()
print(varA)
funcA()
def funcA():
varA = 20
def funcB():
nonlocal varA
varA = 25
funcB()
print(varA)
funcA()
执行上述代码时,输出将为20
,尽管funcB
中的varA
值已更改。重要的是,您已经创建了一个名为varA
的全局变量,并将其分配给值25
,但当您调用print(varA)
时,它引用的是在funcA
中声明的局部变量
要解决此问题,必须使用非本地
关键字,
nonlocal关键字用于处理嵌套函数中的变量,其中变量不应属于内部函数
以代码为例
def funcA():
varA = 20
def funcB():
global varA
varA = 25
funcB()
print(varA)
funcA()
def funcA():
varA = 20
def funcB():
nonlocal varA
varA = 25
funcB()
print(varA)
funcA()
现在,当您执行代码时,输出将如预期的那样为25
现在,来看看你的代码,正确的代码可能是这样的
L = '?'
ignore_letters = ['!', '?', '.', ',']
print("type 'commands' for list of commands")
name = input('hello what is your name :) ')
def main():
global restart
answer = input('please type your request here ' + name + ': ').split()
def answerFunc():
nonlocal answer
if 'list' in answer and 'take' in answer:
print('ok')
i = input('type your list '+name+' ')
l = i.split()
global L
L = l.sort()
main()
elif 'run' in answer and 'list' in answer:
print('ok sure')
print('here ' + L)
main()
answerFunc()
您在代码中错误地使用了
global
关键字
注:
全局关键字用于从非全局范围(例如函数内部)更改或创建全局变量。
以代码为例
def funcA():
varA = 20
def funcB():
global varA
varA = 25
funcB()
print(varA)
funcA()
def funcA():
varA = 20
def funcB():
nonlocal varA
varA = 25
funcB()
print(varA)
funcA()
执行上述代码时,输出将为20
,尽管funcB
中的varA
值已更改。重要的是,您已经创建了一个名为varA
的全局变量,并将其分配给值25
,但当您调用print(varA)
时,它引用的是在funcA
中声明的局部变量
要解决此问题,必须使用非本地
关键字,
nonlocal关键字用于处理嵌套函数中的变量,其中变量不应属于内部函数
以代码为例
def funcA():
varA = 20
def funcB():
global varA
varA = 25
funcB()
print(varA)
funcA()
def funcA():
varA = 20
def funcB():
nonlocal varA
varA = 25
funcB()
print(varA)
funcA()
现在,当您执行代码时,输出将如预期的那样为25
现在,来看看你的代码,正确的代码可能是这样的
L = '?'
ignore_letters = ['!', '?', '.', ',']
print("type 'commands' for list of commands")
name = input('hello what is your name :) ')
def main():
global restart
answer = input('please type your request here ' + name + ': ').split()
def answerFunc():
nonlocal answer
if 'list' in answer and 'take' in answer:
print('ok')
i = input('type your list '+name+' ')
l = i.split()
global L
L = l.sort()
main()
elif 'run' in answer and 'list' in answer:
print('ok sure')
print('here ' + L)
main()
answerFunc()
首先,您将
answer
重新分配给您定义的函数。。。i、 e.answer=input('请在此处键入您的请求'+name+':')
然后def answer():…
…首先,您将answer
重新分配给您定义的函数。。。i、 e.answer=input('请在这里键入您的请求'+name+':')
然后def answer():…
…我刚才试过了,但发生了什么事呢?它说列表对象不可调用吗