473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Input Help

In python, I am looking for some way to decipher an input by the user. If the user inputs "-1,2,1" (without the quotes), how can I break up the input into the numbers between the commas (ie a = -1, b = 2, and c = 1). Thank you.
Mar 30 '07 #1
4 1323
ilikepython
844 Expert 512MB
In python, I am looking for some way to decipher an input by the user. If the user inputs "-1,2,1" (without the quotes), how can I break up the input into the numbers between the commas (ie a = -1, b = 2, and c = 1). Thank you.
Hello
You can use the split function
Expand|Select|Wrap|Line Numbers
  1. string = raw_input("Enter numbers")
  2. string = string.split(",")   # this makes a list with every item in between the 
  3.                                               # given argument
  4.  
  5. for x in range(len(string)):          # make numbers into integers if you'd like
  6.     string[x] = int(string[x])          
  7.  
  8. try:                                  # assign variables to the items in the list and make                                    
  9.     a = string[0]                 # sure program doesn't crash
  10.     b = string[1] 
  11.     c = string[2]
  12. except IndexError:
  13.     pass 
  14.  
  15.  
Does that help?
Mar 30 '07 #2
ghostdog74
511 Expert 256MB
Hello
You can use the split function
Expand|Select|Wrap|Line Numbers
  1. string = raw_input("Enter numbers")
  2. string = string.split(",")   # this makes a list with every item in between the 
  3.                                               # given argument
  4.  
  5. for x in range(len(string)):          # make numbers into integers if you'd like
  6.     string[x] = int(string[x])          
  7.  
  8. try:                                  # assign variables to the items in the list and make                                    
  9.     a = string[0]                 # sure program doesn't crash
  10.     b = string[1] 
  11.     c = string[2]
  12. except IndexError:
  13.     pass 
  14.  
  15.  
Does that help?
try not to use "string" as a variable. string is a module by itself
Mar 31 '07 #3
ilikepython
844 Expert 512MB
try not to use "string" as a variable. string is a module by itself
Oh ok, thanks for the advice.
Mar 31 '07 #4
This fixed my problem. Thank you so much for your help.
Mar 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: reneeccwest | last post by:
Different user input boxes are automatically populated based on a value of the dropdown box. Can anyone help me on that?
3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
5
by: Gregg | last post by:
Hello all, I have been banging my head over a problem that I am having reading a comma seperated file (CSV) that can contain from 1 to 10,000 records. My code snipit is as follows: **Start...
1
by: Ray Z | last post by:
I would like to have someone to help me out with my program. Users are able to add items if they wish for the Monthly Claims Charges System. I did not manage to do the calculation part of the...
2
by: Killer42 | last post by:
The Input #1 statement simply reads in one line from a text file (in this case you INI file) and places the values from it into one or more variables. So what you are reading in this statement is...
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
2
by: Abneo | last post by:
Hi all, I am very new to Javascript and I am in some need of some help. I am creating a site that has a madlib. I got the madlib code but now I want the results of the madlib to display in...
8
by: Bruce A. Julseth | last post by:
I'm "Failing" a trying to create a multicolum text input page. I have found many examples of two column pages where the first column is the field label and the second column is input text box. But,...
1
by: moveitho | last post by:
Hi, I have the following script on my site but need to implement a select menu into the script as it would make it a lot tidier. This script adds the required amount of fields selected by the...
2
by: poreko | last post by:
I am trying to validate the inputs of a form using javascript. The validation is working but the form is not been submitted after the inputs have been checked. I have been unable to find any error....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.