Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:02:30+00:00 2026-06-17T06:02:30+00:00

Maybe this is a very basic question but i am a beginner in python

  • 0

Maybe this is a very basic question but i am a beginner in python and couldnt find any solution. i was writing a python script and got stuck because i cant use python lists effective. i want user to input (number or numbers) and store them in a python list as integers. for example user can input single number 1 or multiple numbers seperated by comma 1,2,3 and i want to save them to a list in integers.
i tried this ;

def inputnumber():
    number =[]
    num = input();
    number.append(num)
    number = map(int,number)
return (number)
def main():
    x = inputnumber()
print x

for a single number there is no problem but if the the input is like 1,2,3 it gives an error:

Traceback (most recent call last):
File "test.py", line 26, in <module>
main()
File "test.py", line 21, in main
x = inputnumber()
File "test.py", line 16, in inputnumber
number = map(int,number)
TypeError: int() argument must be a string or a number, not 'tuple'

Also i have to take into account that user may input characters instead of numbers too. i have to filter this. if the user input a word a single char. i know that i must use try: except. but couldn’t handle. i searched the stackoverflow and the internet but in the examples that i found the input wanted from user was like;

>>>[1,2,3]

i found something this Mark Byers’s answer in stackoverflow but couldn’t make it work
i use python 2.5 in windows.

Sorry for my English. Thank you so much for your helps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:02:31+00:00Added an answer on June 17, 2026 at 6:02 am

    In your function, you can directly convert num into a list by calling split(','), which will split on a comma – in the case a comma doesn’t exist, you just get a single-element list. For example:

    In [1]: num = '1'
    
    In [2]: num.split(',')
    Out[2]: ['1']
    
    In [3]: num = '1,2,3,4'
    
    In [4]: num.split(',')
    Out[4]: ['1', '2', '3', '4']
    

    You can then use your function as you have it:

    def inputnumber():
        num = raw_input('Enter number(s): ').split(',')
        number = map(int,num)
        return number
    
    x = inputnumber()
    print x
    

    However you can take it a step further if you want – map here can be replaced by a list comprehension, and you can also get rid of the intermediate variable number and return the result of the comprehension (same would work for map as well, if you want to keep that):

    def inputnumber():
        num = raw_input('Enter number(s): ').split(',')
        return [int(n) for n in num]
    
    x = inputnumber()
    print x
    

    If you want to handle other types of input without error, you can use a try/except block (and handle the ValueError exception), or use one of the fun methods on strings to check if the number is a digit:

    def inputnumber():
        num = raw_input('Enter number(s): ').split(',')
        return [int(n) for n in num if n.isdigit()]
    
    x = inputnumber()
    print x
    

    This shows some of the power of a list comprehension – here we say ‘cast this value as an integer, but only if it is a digit (that is the if n.isdigit() part).

    And as you may have guessed, you can collapse it even more by getting rid of the function entirely and just making it a one-liner (this is an awesome/tricky feature of Python – condensing to one-liners is surprisingly easy, but can lead to less readable code in some case, so I vote for your approach above 🙂 ):

    print [int(n) for n in raw_input('Number(s): ').split(',') if n.isdigit()]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a very basic question but somehow it got me tricked... when
I know this maybe a very basic question but I'm having a bit of
Maybe I am wrong, but this seems to be a very basic question. Suddenly
Maybe this is a poor question, but, I can't find a tutorial or even
This may be a very basic question, but is it possible to post a
This may be very basic question. But I was wondering about different nancyfx hosting
This may be a very basic question. But I am finding it confusing with
I know this may be a very basic question, sorry if it is but
May be this is a very basic question, but I am really interested to
this may be a very basic question but I haven't used Linq much so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.