简体   繁体   English

创建带有用户提示的输入的列表?

[英]Creating a list with inputs prompted by the user?

I'm a beginner and taking an intro Python course. 我是一个初学者,正在学习Python入门课程。 The first part of my lab assignment asks me to create a list with numbers entered by the user. 我的实验作业的第一部分要求我创建一个列表,其中包含用户输入的数字。 I'm a little confused. 我有点困惑。 I read some other posts here that suggest using "a = [int(x) for x in input().split()]" but I'm not sure how to use it or why, for that matter. 我在这里阅读了其他一些文章,建议使用“ a = [int(x)for input()。split()中的x)”,但我不确定如何使用它或为什么使用它。 The code I wrote before based on the things I've read in my textbook is the following: 根据我在教科书中阅读的内容,我之前编写的代码如下:

 while True: 
  num = int(input('Input a score (-99 terminates): ')) 
   if num == -99: 
   break

Here's the problem from the professor: 这是教授的问题:

Your first task here is to input score values to a list called scores and you will do this with a while loop. 您的第一个任务是将分数值输入到名为scores的列表中,并使用while循环进行此操作。 That is, prompt user to enter value for scores (integers) and keep on doing this until user enters the value of -99. 即,提示用户输入分数(整数)的值,并继续执行此操作,直到用户输入-99的值为止。 Each time you enter a value you will add the score entered to list scores. 每次输入值时,您都会将输入的分数添加到列表分数中。 The terminating value of -99 is not added to the list Hence the list scores should be initialized as an empty list first using the statement: scores = [] Once you finish enter the values for the list, define and called a find called print_scores() that will accept the list and then print each value in the list in one line separate by space. 终止值-99不会添加到列表中,因此应首先使用以下语句将列表分数初始化为空列表:scores = []完成输入列表的值后,定义并调用名为print_scores( ),它将接受列表,然后将列表中的每个值打印在一行中,并以空格分隔。 You should use a for-loop to print the values of the list. 您应该使用for循环来打印列表的值。

I think this will do the job: 我认为这可以完成工作:

def print_scores(scores):
 for score in scores:
  print(str(score), end = " ")
 print("\n")

scores = []
while True:
 num = int(input('Input a score (-99 terminates)'))
 if num == -99:
  break
 scores.append(num)
print_scores(scores)

scores = [] creates an empty array and scores.append() adds the element to the list. scores = []创建一个空数组, scores.append()将元素添加到列表中。

print() will take end = ' ' so that it separates each result with a space instead of a newline ( \\n' ) all while conforming to the requirement to use a loop for in the assignment. print()将采取end = ' ' (以便它的每个结果与一个空间,而不是一个新行分开\\n' )中的所有同时符合要求使用循环for在分配。 str(score) ensures the integer is seen as a string, but it's superfluous here. str(score)确保将整数视为字符串,但是此处多余。

This is actually not an elegant way to print the scores, but the teacher probably wanted to not rush things. 这实际上不是打印分数的一种优雅方法,但是老师可能想不要仓促行事。

So yeah, you want to continually loop a scan, asking for input, and check the input every time. 所以,是的,您想连续循环扫描,询问输入,并每次检查输入。 If it's -99, then break. 如果是-99,则断开。 If its not, append it to the list. 如果不是,请将其附加到列表中。 Then pass that to the print function 然后将其传递给打印功能

def print_list(l):
    for num in l:
        print(num, ' ', end='')
l = []
while True:
    s = scan("enter some number (-99 to quit)")
    if s == "-99":
        break
    l.append(int(s))

print_list(l)

the print(num, ' ', end='') is saying "print num, a space, and not a newline" print(num,'',end ='')在说“ print num,一个空格,而不是换行符”

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

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