Python 名称';usr#U名称';没有定义

Python 名称';usr#U名称';没有定义,python,python-3.x,Python,Python 3.x,在我的while循环中,我想添加我将从usr_名称定义的所有用户名 #!/bin/usr/python3 import os, sys import math try: project_file = open('work_good.txt', 'w+') text = project_file.read() except FileNotFoundError: text = ('File not found') print (text) project

在我的while循环中,我想添加我将从usr_名称定义的所有用户名

#!/bin/usr/python3 
import os, sys
import math

try:
    project_file = open('work_good.txt', 'w+')
    text = project_file.read()

except FileNotFoundError:
    text = ('File not found')
    print (text)
    project_file.close()


def Zone():
    #Introduction to the database
    print ('WELCOME TO THE DATABASE', file=project_file)
    print (23*'=', file=project_file)

def Username():
    #Ask for users username
    usr_name = input ('What do you want your current username to be?: ').strip().capitalize()
    print ('Your username is: {}'.format(usr_name), file=project_file)

while True:
    my_list = []
    if usr_name in my_list:
        my_list.append(usr_name)



Zone()
Username()
错误显示:

回溯(最近一次调用last):文件“project_work.py”,第27行, 在里面 如果我的列表中的usr\U名称:名称错误:未定义名称“usr\U名称”

我曾尝试将字符串添加到列表中,但失败了。 我试图更改变量名(usr_name),但没有成功


我想要它做的是在列表中添加任何用户名,但我不能。如何修复此问题?

usr\u name
Username()函数的范围内定义

因此,它在您尝试使用它的外部不可用。您需要从
Username()
返回
usr\u name

#/bin/usr/python3
导入操作系统,系统
输入数学
def区域():
#数据库简介
打印(“欢迎访问数据库”,文件=项目文件)
打印(23*'=',文件=项目文件)
def Username():
#询问用户用户名
usr_name=input('您希望当前用户名是什么?:').strip().capitalize()
print('您的用户名是:{}'。格式(usr\u名称),file=project\u文件)

返回usr#U名称#非常感谢您的回答!非常感谢您的帮助。
我的\u列表
始终为空,因此
如果我的\u列表中的usr\u名称
始终为
False
#!/bin/usr/python3 
import os, sys
import math


def Zone():
    #Introduction to the database
    print ('WELCOME TO THE DATABASE', file=project_file)
    print (23*'=', file=project_file)


def Username():
    #Ask for users username
    usr_name = input ('What do you want your current username to be?: ').strip().capitalize()
    print ('Your username is: {}'.format(usr_name), file=project_file)
    return usr_name  # <---- return


try:
    project_file = open('work_good.txt', 'w+')
    text = project_file.read() 
except FileNotFoundError:
    text = ('File not found')
    print (text)
    project_file.close()

Zone()   
while True:
    my_list = []
    usr_name = Username()  # <---- get usr_name from function
    if usr_name in my_list:
        my_list.append(usr_name)