简体   繁体   English

如何使用用户输入来确定程序的工作方式?

[英]How do I use user input to determine how the program works?

this is my first post here on stackoverflow and this is my first time using Python to make a program.这是我在 stackoverflow 上的第一篇文章,也是我第一次使用 Python 编写程序。 I want to make a program to ask the user to input names of students then be able to add more information to that student as the program continues to run.我想制作一个程序,要求用户输入学生的姓名,然后在程序继续运行时向该学生添加更多信息。 Ideally I want the user to choose how many students there are, choose how many rounds are played, give them names, assign scores, and at the end output the final score and the averages of each student.理想情况下,我希望用户选择有多少学生,选择玩多少轮,给他们起名字,分配分数,最后输出最终分数和每个学生的平均分。

This is what I have currently:这是我目前所拥有的:

name_students = list()
num_students = input("How many students?: ")
competitionRounds = input("How many rounds: ")
score_students = list ()
myList = name_students

for i in range(1, int(num_students) + 1):
name_students.append(input("Student Name: "))
print(name_students)


for i in range (1, int(competitionRounds)):
print(input(name_students [0] + " Total Score for each round: "))

This is how the program runs:这是程序运行的方式:

How many student?: 3 #Have the user input answers to these questions
How many rounds?: 2
Student Name: Nick
Student Name: Bob
Student Name: Lisa
Nick Total Score for each round:

I was trying to get it to ask for every name listed like我试图让它询问列出的每个名字

Nick Total Score for round 1:
Bob Total score for round 1:
Lisa Total score for round 1:
Nick Total score for round 2:

Etc.等等。

I appreciate anyone who replies.我感谢任何回复的人。

---Edit--- So I now have problems taking the numbers inputted by the user and adding them together by the name placed by the user. ---编辑--- 所以我现在在获取用户输入的数字并按用户放置的名称将它们加在一起时遇到问题。

My expected outcomes is: Nick Total Score for round 1: 2 Bob Total score for round 1:3 Lisa Total score for round 1:1 Nick Total Score for round 2:2 Bob Total score for round 2:3 Lisa Total score for round 2:1 Nick Total score for all rounds: 4 Etc我的预期结果是: Nick 第 1 轮总得分:2 Bob 第 1 轮总得分 1:3 Lisa 第 1 轮总得分 1:1 Nick 第 2 轮总得分 Bob 第 2 轮总得分 Lisa 第 2 轮总得分2:1 尼克 各轮总分:4 等

Currently my code looks like:目前我的代码看起来像:

 name_students = list()
 num_students = input("How many students?: ")
 competitionRounds = input("How many rounds: ")
 score_students = []
 myList = name_students
 total_scores = 0, []


 for i in range(1, int(num_students) + 1):
   name_students.append(input("Student Name: "))

 for i in range (1, int(competitionRounds) +1):
    for j in range(len(name_students)):
     score_students.append(input(name_students [j] + " Total Score for round " + str(i) +": "))

 for i in range (1,int(competitionRounds) +1):
    for t in range(len(score_students)):
      print(name_students + score_students + total_scores)

You need to change the last loop您需要更改最后一个循环

for i in range (1, int(competitionRounds)):
    for j in range(len(name_students)):
         score_students.append(input(name_students [j] + " Total Score for round " + str(i) + ": "))

This will ask the user for each student's score for each round, and keep appending that to score_students .这将询问用户每轮每个学生的分数,并继续将其附加到score_students You may then manipulate that the way you want.然后你可以按照你想要的方式操纵它。

How many students?: 2
How many rounds: 3
Student Name: A
Student Name: B
['A', 'B']
A Total Score for round 1: 2
B Total Score for round 1: 1
A Total Score for round 2: 2
B Total Score for round 2: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM