如何在python中打印多个输入的输出

2024-05-09 16:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

n=int(input("Enter the number of User:\n"))
for i in range(0,n):
    print("Enter the details of User %d"%(i+1))
    name=input("Enter the name of the user:\n")
    mno=int(input("Enter the mobile number of the user:\n"))
    uname=input("Enter the username of the user:\n")
    pswd=input("Enter the password of the user:\n")

我无法打印此代码 如何打印此代码的输出**


Tags: ofthe代码nameinnumberforinput
3条回答
n=int(input("Enter the number of User:\n"))
output=[]
for i in range(0,n):
    print("Enter the details of User %d"%(i+1))
    name=input("Enter the name of the user:\n")
    mno=int(input("Enter the mobile number of the user:\n"))
    uname=input("Enter the username of the user:\n")
    pswd=input("Enter the password of the user:\n")
    output.append({
       "name":name,
       "mno":mno,
       "uname":uname,
       "pswd":pswd
    })
print(output)

输出

Enter the number of User:
1
Enter the details of User 1
Enter the name of the user:
reza
Enter the mobile number of the user:
1212
Enter the username of the user:
rb
Enter the password of the user:
23232

[{'name':'reza','mno':1212,'uname':'rb','pswd':'23232'}]

试试这个

input_data = []
for i in range(0, n):
    print("Enter the details of User %d" % (i + 1))
    name = input("Enter the name of the user:\n")
    mno = int(input("Enter the mobile number of the user:\n"))
    uname = input("Enter the username of the user:\n")
    pswd = input("Enter the password of the user:\n")

    input_data.append({
        "name": name,
        "mno": mno,
        "uname": uname,
        "pswd": pswd,
    })

print(input_data)```

我相信您正在尝试累积用户数据,以便在最后打印所有数据(如果不是这样,您可以在for循环的底部添加一行print(f"User {i+1} is named {name} with mobile number {mno} (username {uname}, password {pswd}"))。我认为这里有两种方法,其中一种更好。更糟糕但更简单的方法是将每个数据点存储为单个数组:

name = []
mno = []
uname = []
pswd = []
n=int(input("Enter the number of User:\n"))
for i in range(0,n):
    print("Enter the details of User %d"%(i+1))
    name.append(input("Enter the name of the user:\n"))
    mno.append(int(input("Enter the mobile number of the user:\n")))
    uname.append(input("Enter the username of the user:\n"))
    pswd.append(input("Enter the password of the user:\n"))
print(f"names: {name}\nPhone Numbers: {mno}\nUsernames: {uname}, Password: {pswd}")
# This information could be processed later in the program with name[0], mno[0]

更好的替代方法是使用Python的OOP模型,创建一个带有__init__的用户类,该类将分别设置名称、mno、uname、pswd的内部变量作为输入,并将其存储在self中,如果在类中添加了__repr____str__接口,则允许您只编写print(user)

相关问题 更多 >