0

im new to python and im having a bit of difficulty with a beginner question

Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers

and this is the code i wrote

sea =  9 , 8 , 8 , 8

list = sea.split(",")
tuple = tuple(list)

print ("List: " ,list)
print ("Tuple: " ,tuple)

but its giving me an Attribute Error

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

thats it hehe, thank you for taking time for reading this, please help me correct it :0

1
  • 1
    Just so you know, sea is already a tuple. Commented May 3, 2021 at 17:46

7 Answers 7

4

Now, the sea variable holds a tuple of (9, 8, 8, 8).

To accept input from user, use input() function. The input() function returns a string. so you can use str.split(',') to split it.

Also, don't use list or tuple for variable names, you're shadowing the built-in functions.

The full code:

input_from_user = input()

l = [int(n) for n in input_from_user.split(",")]
t = tuple(l)

print("List: ", l)
print("Tuple: ", t)

Output (for example):

1,2,3,4
List:  [1, 2, 3, 4]
Tuple:  (1, 2, 3, 4)
Sign up to request clarification or add additional context in comments.

1 Comment

tysm for the answer now i get it :)
2

You should wrap content of the sea varible with ".
Your error happends because 9 , 8 , 8 , 8 is not a string by default. It is, as error says, tuple. By wrapping it like this "9 , 8 , 8 , 8" it will become string and your code will work.

Comments

1

When you enter sea = 9,8,8,8 it becomes sea = (9,8,8,8). It's a Tuple.

Try this Code

SEA = "9,8,8,8"
LIST = [int(x) for x in SEA.split(",")]
TUPLE = tuple(LIST) #Directly Convert List as a Tuple
print("List",LIST)
print("Tuple",TUPLE)

NOTE: Don't use reserved words as variables

Comments

0

Python interprets objects separated by commas as a tuple. If you were to print the type of sea, you'd get tuple.

The .split() function is for str objects. It's not applicable for tuples.

Also, based on the prompt you were given, you should be using sea =input() instead of defining the numbers. This will allow the person running the program to choose the numbers.

Additionally, try not to use variable names like list or tuple or a name that could override the built-in functions. This can cause some unwanted errors in your code.

Comments

0

Rewrite your code as following.

sea =1,2,3,4

list1 =list(sea)
tuple1 =sea

print ("List: " ,list1)
print ("Tuple: " ,tuple1)

Don't use keywords as variable names, it may confuse the interpreter It is storing the values by default in tuple

Comments

0

Let me explain this to you, The way in which you are taking the input

sea = 9 , 8 , 8 , 8

The value in the sea variable becomes tuple.

Later when you apply this function,

list = sea.split(",")

It will throw an error, due the reason that tuple do not have split function, string has this built-in function

If you want user input you can take input in this way which is going to accept a string:

sea = input()

Or if you want hard coded values you can use double quotes or single quotes around them to make them string:

sea =  "9 , 8 , 8 , 8"

So your code should look like this :

sea =  input()

l_list = sea.split(",")
t_tuple = tuple(l_list)

print ("List: " ,l_list)
print ("Tuple: " ,t_tuple)

Also do not use reserved words and built-in function names as variables otherwise they will override the built-in functions and keywords.

Comments

0

Your going to have to take a string input.

sea = input('Enter: ')
list = sea.split(",")
tuple = tuple(list)

print ("List: " ,list)
print ("Tuple: " ,tuple)

This wont work

sea =  9 , 8 , 8 , 8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.