简体   繁体   English

如何将用户输入转换为列表

[英]How to convert user input into a list

I'd like to write a python program that asks the user to input 7 numbers.我想编写一个 python 程序,要求用户输入 7 个数字。 This input should be converted into a list and display the individual numbers first, and then display the list as a whole.此输入应转换为列表并先显示单个数字,然后再显示整个列表。 So my thought was i'd start with a for loop.所以我的想法是我会从一个 for 循环开始。

for a in range(1,8):
    number = int(input("please enter a number"))
    b = [a]
    print(b)

So it should look like this:所以它应该是这样的:

please enter number: 4
4
please enter number: 14
14
please enter number: 25
25

(repeat 4 more times) (再重复4次)

And in the end:最后:

Your list is: [4, 14, 25, ...]
result = []

for a in range(1,8):
    number = int(input("please enter a number: "))
    result.append(number)

print(result)

A sample output is like:示例输出如下:

please enter a number: 1
please enter a number: 2
please enter a number: 3
please enter a number: 4
please enter a number: 5
please enter a number: 6
please enter a number: 7
[1, 2, 3, 4, 5, 6, 7]
Numbers = []

for a in range (1,8):
    number = int(input("Please enter a number:"))
    Numbers.append(number)
    print(number)

Assuming that you don't need to display a list every time (see my comment):假设您不需要每次都显示列表(请参阅我的评论):

b = []
for a in range(7):
    number = int(input("please enter a number: "))
    b.append(number)
    print(number)
print (b)

So, it sounds like you're fairly new to Python or otherwise unfamiliar with the built-in data structures.因此,听起来您对 Python 相当陌生,或者不熟悉内置数据结构。 It's always obligatory to recommend the documentation , though, if you're new to computer programming, and not just Python's data structures, there are other resources that are valuable to learning, as well.推荐文档总是有必要,但是,如果您是计算机编程的新手,而不仅仅是 Python 的数据结构,还有其他对学习很有价值的资源

For the most complete answer to the question, which includes string formatting and escape sequences, this method will get the exact output.对于该问题的最完整答案,包括字符串格式和转义序列,此方法将获得准确的输出。

def collect_user_input():
    list_of_numbers = []
    for x in range(7):
        number = int(input("Enter a Number: "))
        list_of_numbers.append(number)
        print("\nYour number was: %d\n" % number)
    print("Your list is: \n", list_of_numbers)

collect_user_input()

You can also get all numbers in one go:您还可以一次性获取所有数字:

nums = input("Please input space seperated numbers - as much as you need: ")
splitted = list(map(int, (x for x in nums.split() if x.isdigit()) ) )

print(splitted)

Output for input "5 4 55 6 asdf 923 7.23" :输入"5 4 55 6 asdf 923 7.23"

Please input space seperated numbers - as much as you need: 5 4 55 6 asdf 923 7.23
[5, 4, 55, 6, 923]

This drops any non-integer inputs being inputted automatically and converts the rest to integers.这会丢弃任何自动输入的非整数输入并将其余输入转换为整数。

You can limit the parsed numbers fe by:您可以通过以下方式限制解析的数字 fe:

splitted = splitted[:7]  # 7 numbers at most - might be less - you need to check with if

Doku:独行:


Explanation for list(map(int, (x for x in nums.split() if x.isdigit()) ) ) : list(map(int, (x for x in nums.split() if x.isdigit()) ) )

  • creates a list创建一个列表
    • from applying int()从应用int()
      • to each element of split() of your input到输入的split()的每个元素
        • only if the splitted part x is True if tested with isdigit()仅当使用isdigit()测试时isdigit()部分xTrue
l1=[]  
for i in range(1,11):  
  inp=int(input("enter a number"))  
  l1.append(inp)  
print(l1)  

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

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