0

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
AMC
  • 2,642
  • 7
  • 13
  • 35
Jordan
  • 21
  • 3
  • Does this answer your question? [Add Variables to Tuple](https://stackoverflow.com/questions/1380860/add-variables-to-tuple) – user120242 Jun 29 '20 at 23:49
  • tuples are [immutable](https://en.wikipedia.org/wiki/Immutable_object). This is by design. What you want is a list, or to construct a new tuple. This could be done on one line using a comprehension – user120242 Jun 29 '20 at 23:50
  • `num = value` makes `num` hold whatever value `value` holds. `num`'s previous tuple contents are no longer relevant in any way. – jasonharper Jun 29 '20 at 23:53
  • as @jasonharper points out, I'm guessing you probably tried to do num[counter] = value, which is not allowed, because tuples are enforced immutable, so you tried to do num=value expecting it to fill the tuple, which doesn't work as his comment explains. you should be using `num = ()` and then `num += value,`. I'm not sure I understand why you did counter_second = num[value] though, that doesn't make sense at all, unless you are actually trying to create a unzipped set of [order, num], for which you should be using a dict – user120242 Jun 29 '20 at 23:58
  • _printing of the tuple does not work_ ... _printing of the list does work_ What does that mean? Please provide a [mcve], and see [ask], [help/on-topic]. – AMC Jun 30 '20 at 01:55

1 Answers1

0

The tuples in python are immutable i.e 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
  • Thank you, i just converted the list to a tuple. I suppose i could have done it another way in which i store the value. then instantiate and fill the tuple. I am still learning. – Jordan Jun 30 '20 at 20:14
  • I can not upvote because i dont have enough reputation points. sorry. – Jordan Jul 04 '20 at 19:47