473,320 Members | 2,189 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,320 software developers and data experts.

Function to take input as string, input parameter to function

Ok, so I am almost done with this question:
Write and test a function to take as input a list of strings (each of which
represents a number) and to return a list of numeric values. For example, if
the input to the function is [“67”, “8”,”75”] and return [67,8,75]. The input
should be as a parameter to the function.


here is the code I have written:
Expand|Select|Wrap|Line Numbers
  1. def func( num1, *numtuple):
  2.     print num1
  3.     for num in numtuple:
  4.         print num
it works fine except for the fact that the input HAS to be as a string. Any help would be great because I am stumped.

Cheers
Sep 4 '10 #1
1 2884
bvdet
2,851 Expert Mod 2GB
The instructions require the function argument to be a list of strings as in
Expand|Select|Wrap|Line Numbers
  1. ['1','2','3']
This can be done two ways.
Expand|Select|Wrap|Line Numbers
  1. def f(*args):
  2.     # Accept a variable number of arguments
  3.     pass
  4.  
  5. def f(strList):
  6.     # Accept a single argument
  7.     pass
Your function must return a list of numbers. This can be done several ways, but you must use the return statement as in
Expand|Select|Wrap|Line Numbers
  1. def f(*args):
  2.     # Calculate somevalue
  3.     return somevalue
Assuming the numbers are all integers, here are some examples:
Expand|Select|Wrap|Line Numbers
  1. >>> strList = ['1','2','3']
  2. >>> map(int, strList)
  3. [1, 2, 3]
  4. >>> [int(n) for n in strList]
  5. [1, 2, 3]
  6. >>> result = []
  7. >>> for n in strList:
  8. ...     result.append(int(n))
  9. ...     
  10. >>> result
  11. [1, 2, 3]
  12. >>> 
HTH
Sep 4 '10 #2

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

Similar topics

2
by: Tom | last post by:
I'm trying to find a good, solid function that sanitizes string values for form validation. The user notes on php.net offer the following: ...
2
by: sbox | last post by:
I've got an error "System.FormatException: Input string was not in a correct format." while I'm implementing a datagrid and a textbox What's wrong with it? Sub Button1_Click(sender As Object, e...
8
by: Ottar | last post by:
I have a few numeric fields, and when I update i get the error: "Input string was not in a correct format". Next line:" System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
4
by: priyanka | last post by:
Hi, I want to input a string from command line. I use the following program to input the string. #include<stdio.h> int main(){ char input;
9
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
6
by: CSINVA | last post by:
I need to be able to pass a variable to a function and then have the function return me a variable. I need to pass GetUsrID Tom and get back the corresponding UserID associated with tom by...
11
by: Siol | last post by:
I'm trying to call a dll function from C# which has the following form: int some_function(int count, char **arg1, char **arg2) Which parameter type I need to use in C# for C++ char** type? I...
2
by: John Murtari | last post by:
Folks, Been playing around with developing a 'catch all' convenience function that could be used to properly screen form input via a browser to a script. We are ASSuming register globals is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.