简体   繁体   English

它说 UnboundLocalError,在此代码中分配变量后如何更新变量?

[英]It say UnboundLocalError, How can i update variable after assigning it in this code?

name = "Aayan"

def AI():
    print("Hi, {}. I hope you are doing well :)".format(name))
    ask = input("How can i help you, {} ? : ".format(name))
    if ask == "change name" or "rename":
        new_name = input("What should I call you ? : ")
        name = new_name
    else:
        exit()

AI()

It throws UnboundLocalError: local variable 'name' referenced before assignment.它抛出 UnboundLocalError: local variable 'name' referenced before assignment。 How can I update name after asking it from the user?在向用户询问名称后如何更新名称?


name = "Aayan"

def AI():
    global name
    print("Hi, {}. I hope you are doing well :)".format(name))
    ask = input("How can i help you, {} ? : ".format(name))
    if ask == "change name" or "rename":
        new_name = input("What should I call you ? : ")
        name = new_name
        return name
    else:
        exit()


print(AI())

use global keyword inside function and call name在 function 中使用全局关键字并调用名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这段代码会引发 UnboundLocalError? 我该如何解决这个问题? - Why does this code raise an UnboundLocalError? How can I fix the problem? 为什么它说 UnboundLocalError: local variable 'max_range' referenced before assignment 当我分配它时 - Why does it say UnboundLocalError: local variable 'max_range' referenced before assignment When i have assigned it 如何修复我的代码中的“UnboundLocalError:分配前引用的局部变量“dc”? - How do I fix 'UnboundLocalError: local variable 'dc' referenced before assignment' in my code? 如何从函数中解决UnboundLocalError? - How can I solve UnboundLocalError out of the function? 我该如何解决:UnboundLocalError:分配前引用的局部变量“生成” - how can i fix: UnboundLocalError: local variable 'generate' referenced before assignment 如何修复“UnboundLocalError:分配前引用的局部变量'user_score'”? - How can I fix "UnboundLocalError: local variable 'user_score' referenced before assignment"? 我如何让我的不和谐机器人说出一个人的名字,然后在他们猜出以聊天形式键入的变量后说出正确的名字 - How can I get my discord bot to mention a persons name then say correct after they guess a variable in the form of typing it in the chat 如何在没有全局变量的情况下解决“ UnboundLocalError:赋值之前引用了本地变量'foo'”错误? - How can I get around “UnboundLocalError: local variable 'foo' referenced before assignment” errors without global variables? 如何解决此错误“UnboundLocalError:分配前引用的局部变量'a'” - How can I fix this error “UnboundLocalError: local variable 'a' referenced before assignment” 如何使 dataframe 全球化? 出现错误 - UnboundLocalError:分配前引用的局部变量 - How can I make a dataframe global? Getting error - UnboundLocalError: local variable referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM