在python 3.7中遍历列表

时间:2019-07-02 17:47:44

标签: python python-3.x

简单的PYTHON惯例

此程序是关于将新用户添加到列表中,并检查用户名是否可用或已被使用...

current_users =['john','jack','blake','hussain','ravi','rose']

new_users = []
inp = int(input('Enter how many users you want to add:')
for new in range(inp):
    new_user = input('Enter the new user to add to the list:')

    if new_user in current_users:
        print('The name',new_user,'was already taken by someone else!')
    else:
        print('The username',new_user,'is available\n Adding user',new_user,'to the list...')
        new_users.append(new_user)
        print(new_users)
        current_users.extend(new_users)

我期望的是首先从用户那里获取范围值,并将其存储到变量inp并将其传递给下一行中称为range(inp)的函数。通过使用for循环,将新用户添加到名为new_users []的列表中,并将该列表附加到current_users上,但是我在for关键字行中得到了无效的语法。我不知道会有什么帮助

1 个答案:

答案 0 :(得分:4)

您缺少右括号。

inp = int(input('Enter how many users you want to add:')

inp = int(input('Enter how many users you want to add:'))