简体   繁体   English

如何从用户接受逗号分隔的数字序列并生成列表和元组?

[英]How to accept a sequence of comma-separated numbers from user and generate a list and a tuple with them?

I am attempting to insert values into this tuple from input given by the user although i am failing to complete this task.我试图从用户给出的输入中将值插入到这个元组中,尽管我没有完成这个任务。 A simple error must be contributing.一个简单的错误一定是有贡献的。

order = ['first', 'second', 'third', 'fourth'] # 4 values are to be accepted
numbers = []  # the array to hold the list values
num = ("", "", "", "") # a initiated  tuple to store the values

for accord in order: 
    value = input("please enter a  value : " ) # User enter value
    numbers.append(value)
    counter = 0   # counter to accept more values to the tuple
    counter_second = num[value]
    counter += 1 
    num = value # 

print(num) # printing of the tuple does not work
print(numbers) # printing of the list does work

The tuples in python are immutable ie once you assign it a value you cannot change. python 中的元组是不可变的,即一旦为它分配了一个值,就无法更改。 If you really need a tuple you might have to create a list and then convert it to tuple.如果您真的需要一个元组,您可能必须创建一个列表,然后将其转换为元组。

order = ['first', 'second', 'third', 'fourth'] # 4 values are to be accepted
numbers = []  # the array to hold the list values
num = ("", "", "", "") # a initiated  tuple to store the values
 

for accord in order: 
    value = input("please enter a  value : " ) # User enter value
    numbers.append(value)
num = tuple(numbers)

print(num) # printing of the tuple does not work
print(numbers) # printing of the list does work

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

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