简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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