我想为刚刚注册帐户的每个用户添加一个 id。 id 的工作方式就像第一个用户注册将有 1(number) 作为他或她的 id,第二个用户将有 2,第三个将有 3,依此类推。第一个用户 id 为 1 没有问题,但是当第二个、第三个和更多用户注册时,他们的 id 仍然保持为 1。有没有办法让我继续为下一个用户 id 添加 1?
customer_list = []
def customer_page():
print('customer page accessed')
opt = input("press '0' to signup another account: ")
if opt == '0':
signup()
def signup_success(f,i,n,p):
f = 1
if f == 1:
i += 1
customer_list.append([i,n,p])
print(customer_list)
customer_page()
def signup():
username = input('please enter your username: ')
password = input('please enter your password: ')
flag = 0
id = 0
signup_success(flag,id,username,password)
signup()
最佳答案
注意你的 customer_list
的长度始终等于您添加的最后一个客户的 ID。您可以简单地使下一个客户的 id 等于 customer_list
的长度。加一。
customer_list = []
def customer_page():
print('customer page accessed')
opt = input("press '0' to signup another account: ")
if opt == '0':
signup()
def signup_success(f,n,p):
f = 1
if f == 1:
i = len(customer_list) + 1
customer_list.append([i,n,p])
print(customer_list)
customer_page()
def signup():
username = input('please enter your username: ')
password = input('please enter your password: ')
flag = 0
signup_success(flag,username,password)
signup()
我试图保持其他一切不变,但老实说我不确定你的 flag
变量是为了。
关于python - 如何将 +1 添加到函数之间的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67242730/