User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 423,548 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,934 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums

Starting Python

Join Date: Jul 2005
Location: France
Posts: 1,005
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 45
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Veteran Poster

Re: Starting Python

  #46  
Feb 12th, 2006
This code shows how to input a series of integers each separated by a space and then convert that input to a list of those integers:
# input a sequence of integer numbers separated by a space and convert to a list

def get_integer_list():
    while True:
        # get the sequence string and strip off any trailing space (easy mistake)
        str1 = raw_input("Enter a sequence of integers separated by a space: ").rstrip()
        try:
            # create a list of integers from the string using list comprehension
            return [int(num) for num in str1.split(" ")]
        except ValueError:
            print "Error: '%s' is not an integer! Try again." % num
            
print get_integer_list()
For example the input 1 2 3 4 5 turns into [1, 2, 3, 4, 5]
Reply With Quote  
All times are GMT -4. The time now is 6:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC