语法错误名“用户名”是没有定义的Python 3

问题描述 投票:-1回答:3

我的问题是我有多个高清报表登录注册骰子。和IM试图调用从登记表骰子声明一个变量,但它说当它的用户名没有定义。

def register():# registers users
    myfile = open ('User names.csv','a+')
    print('\n~~Registration~~\n\n')
    username = input('Enter your username\n')
    password = input('Enter your password\n')
    new_data = username+','+password
    myfile.write (str(new_data))

def dice():
    player_1_username = username

它说名“用户名”没有定义

python-3.x
3个回答
1
投票

变量名可以被限制只能注册功能,而不是function.You必须通过用户名来切割功能作为一个参数或使用户名必须是全球性的功能外,以访问骰子中访问。


0
投票

我想你应该让你的“用户名”变量全球。

   global username
   username = input()

0
投票

那么试试这个:

  def register():# registers users
            myfile = open ('User names.csv','a+')
            print('\n~~Registration~~\n\n')
            username = input('Enter your username\n')
            password = input('Enter your password\n')
            new_data = username+','+password
            myfile.write (str(new_data))
            return username

  def dice():
            player_1_username = register()
  dice() #you must call the function too
© www.soinside.com 2019 - 2024. All rights reserved.