【问题标题】:python using the loop and input user name [duplicate]python使用循环并输入用户名[重复]
【发布时间】:2015-09-14 16:39:58
【问题描述】:

如何编写一个要求用户输入多个学生姓名并打印出姓名的循环。

第一次要求用户输入要输入多少个学生姓名,然后循环输入要求学生姓名的数字。

请看下面的代码。我的挑战是包含在循环中。

n = eval(input("How many student do you have to enter: "))
a = (input("Enter the student name: "))

print (a)

【问题讨论】:

标签: python loops python-3.x


【解决方案1】:
students = []
n = int(input('How many student do you have to enter:'))
for x in range(n):
    name = input('Enter the student name:')
    dob = input('Enter the student dob:')
    students.append({'name': name, 'dob': dob})
print('\n'.join(['name: {0}\ndob: {1}'.format(x.get('name'), x.get('dob')) for x in students]))

【讨论】:

  • 为什么不使用for 循环?手动增加 while 中的计数器更有可能引起拼写错误或非一个错误。
  • 好主意 :) 已更新
  • 如何添加另一个 DOB 输入?
  • 这太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多