Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

python - Use values from another function python3

I have this code, but i dont know how to use the "age" from the user in the other function any clue what i have wrong?

def accounts():
    yourCode = input("Please enter your user code. 
If you already have an account, type your account user code. 
Otherwise, enter a new one to create an account: ")

   
    with open("users.txt", "r") as rf:
        users = rf.readlines() 
        for each_user in [user.split(",") for user in users]: 
            if each_user[0] == yourCode: 
                print(f"Welcome {each_user[1]}") 
                age = each_user[2]
                xxApp() 
                return None 
    with open("users.txt", "a") as af:
        name = input("Please enter your name: ")
        age = input("Enter your age: ")
        af.write(f"{yourCode},{name},{age}
") 
        print(f"Thank you {name}, your information has been added.")
        xxApp()


def xxApp():
    age = each_user[2]
    print(age)


question from:https://stackoverflow.com/questions/65862212/use-values-from-another-function-python3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pass it as a parameter

def accounts():
    yourCode = input("Please enter your user code. 
If you already have an account, type your account user code. 
Otherwise, enter a new one to create an account: ")

   
    with open("users.txt", "r") as rf:
        users = rf.readlines() 
        for each_user in [user.split(",") for user in users]: 
            if each_user[0] == yourCode: 
                print(f"Welcome {each_user[1]}") 
                xxApp(each_user) 
                return None 
    ...


def xxApp(user):
    age = user[2]
    print(age)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...