0

I am trying to do a two things with this program.

Firstly, check if the input is in the list,

and secondly, to remove that item from the list after its inputed, thus changing the list and allowing the program to happen again. I want to be able to eventually remove all items from the list.

I am very new to coding, all help is very welcome. Thanks so much!

lista = (1,2,3,4,5,6,7,8,9,10)
def aturn ():
    print("\n\n\nPlayer A's turn...")
    numA = int(input())
    if numA in lista:
        print ("Yes")
        lista.remove(str(numA))
    else:
        print("No")
    aturn()
        
aturn()

When I run this code. I get the following error and I am not sure why.

AttributeError: 'tuple' object has no attribute 'remove'

  • 1
    In your own words, where the code says `lista.remove(str(numA))`, what do you thin kthat means? In particular, what do you think the `str` part means? What things are in `lista` to begin with? Could any of them ever be equal to `str(numA)`? Why or why not? – Karl Knechtel Jan 24 '23 at 15:01
  • `tuple`s are immutable, `list`s are mutable. Make `lista` a `list` instead if you want to remove elements from it. – Fractalism Jan 24 '23 at 15:01
  • 1
    In your own words, where the code says `lista = (1,2,3,4,5,6,7,8,9,10)`, what do you think this means? Specifcially, what do you think the `()` indicate? Is this a list? (Hint: did the error message use the word "list"?) Do you want to make a list instead? Do you know the syntax for that? – Karl Knechtel Jan 24 '23 at 15:01
  • `lista = [1,2,3,4,5,6,7,8,9,10]` – Unmitigated Jan 24 '23 at 15:01
  • What reason do you have to disagree w/ the error message, and believe that a tuple *does* have a `remove` attribute? – Scott Hunter Jan 24 '23 at 15:02

3 Answers3

0

This is because you are using a tuple instead of a list. A tuple is an immutable data structure. To use a list, you have to declare it with "[]" instead of "()" which is a tuple.

Something like this:

this_is_a_list = [1,2,3,4]
this_is_a_tuple = (1,2,3,4)
0
elista = [1,2,3,4,5,6,7,8,9,10] # You've used tuple instead of list here () - tuple and [] - list
def aturn ():
   print("\n\n\nPlayer A's turn...")
   numA = int(input())
   if numA in lista:
       print ("Yes")
       lista.remove(str(numA))
   else:
       print("No")
   aturn()
    
aturn()
Deep Bhatt
  • 313
  • 1
  • 11
0

In this code lista is of type Tuple. Either you create

lista = [ 1,2,3,4,5,6,7,8,9,10 ]

using [] or the tuple has to converted into a list before removing an item.

This is because Tuples are immutable.

tetris programming
  • 809
  • 1
  • 2
  • 13