为什么python set不能处理input str?[已关闭]

gr8qqesn  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(108)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
15小时前关门了。
Improve this question
我是初学者,我试图让我的用户名写在uniqe名单。

#User base
user='on'
while user=='on':
    user_0={}
    users=[]
    for user_number in range(5):
        user_0={'Name':'','Last name':'',"Age":"","Adres":"","Phone number":"",}
        name_request=input("Please input your name...")
        user_0["Name"]=name_request
        last_name=input("Please input your last name...")
        user_0['Last name']=last_name
        age_request=input("Please input your age...")
        user_0['Age']=age_request
        adres_request=input("Please input your adres")
        user_0['Adres']=adres_request
        phone_number_request=input("Please input your phon number...")
        user_0['Phone number']=phone_number_request
        users.append(user_0)
        for k,info in user_0.items():
            print(f"{k}:{info}")
    print(f"\nUser number:{len(users)}")
    print(f"Here are the information about the user:")
    for user in users:
        print(f"{user['Name']}")
        for k,v in user.items():
            print(f"{k}:{v}")
    print("All the user's names are:")
    for user in users:
        print(set(user['Name']))`

但是无论我怎么尝试set()和其他方法都不起作用。请帮助!
`print(“所有用户的名字都是:“)for user in users:print(set(user['Name']))``

fslejnso

fslejnso1#

如果你想让你的用户在名字上是唯一的,而不是把他们放在set中(你不能把字典放在set中,因为它们是可变的),你可能想让users本身成为一个字典,用名字作为键:

users = {}
for _ in range(5):
    name = input("Please input your name...")
    users[name] = user = {'Name': name}
    user['Last name'] = input("Please input your last name...")
    user['Age'] = input("Please input your age...")
    user['Adres'] = input("Please input your adres")
    user['Phone number'] = input("Please input your phon number...")
    for k, info in user.items():
        print(f"{k}:{info}")
print(f"\nUser number:{len(users)}")
print(f"Here are the information about the user:")
for name, user in users.items():
    print(name)
    for k,v in user.items():
        print(f"{k}:{v}")
print("All the user's names are:", *users, sep='\n')

在一个dict中,键总是唯一的(就像集合中的项一样--集合有点像没有值的dict),所以通过将用户放入按名称键控的dict中,就不可能有多个用户具有相同的名称。
这也使得你可以很容易地找到一个给定的用户dict给定他们的名字:

name = input("What user do you want to look up?")
if name in users:
    for i in user[name].items():
        print(':'.join(i))
else:
    print("No such user.  Known users are:", *users)

相关问题