简体   繁体   中英

Creating a list from a string that contains numbers separated by commas; Python 3

Hi so first I would like to say this is for homework so I do not want to straight answer. The problem is:

This task will implement the functionality of creating a list from a string that contains numbers separated by commas (with possibly blank space between them). For example the string "1,2,3, 4.5, 6.7, 8" would become the list: [1, 2, 3, 4.5, 6.7, 8].

Write the following functions:

is_numeric() - This function has a string parameter and returns True if all the characters in the string are digits, comma, space, or dot. If there are any other characters, the function should return False.

string_to_list() - This function takes a string parameter and returns the list of the numbers in the string. First it should call the is_numeric() function to check that the string has no bad characters (eg letters). If there are any bad characters, it should return the empty list. If there are no bad characters it should try to build the list out of the data in the string. For this it should look at every substring between two consecutive commas. If there is no dot in that substring, then the substring should be converted to an integer. If there is exactly one dot (no more, no less) it should be converted into a float. If any of the substrings between two consecutive commas can not be converted to an int or a float (eg "4.5.8" as it has too many dots), the function should still return the empty list. Hint: the split() method may be useful for this task.

main() - The main() function that will get a string from the user, it will then call the string_to_list() function to build a list out of the user string and then print the resulting list. It will next ask the user if they want to continue. If they want to continue, they should enter 'y'. In that case the function (main) should repeat the previous steps: ask the user for an input, convert it to a list, ask the user again if they want to continue. And so on until the user does not want to continue, in which case he or she should enter 'n'.

The only piece of code I do have checks only the first letter that is in the string to see if it is a digit or a letter. I need to check whether if there is a letter, number, comma, period, or space. I got really lost reading this program. I know I need to get a string from the user, then make that string into a list if only everything is the string is one of the acceptable inputs (letter, number, comma, period, space) Below is my code so far and what my code will be reading in the shell.

s = input("Enter a set of numbers (integers or floats) separated by comma:")

def is_numeric(s):
    for char in list(s):
        if char.isdigit():
            return True
        if char.isalpha():
            return False
            break


if is_numeric(s) == True:
    print(s)
else:
    print('Error')

================================ RESTART ================================

Enter a set of numbers (integers or floats) separated by comma:1,2,3,4
1,2,3,4

================================ RESTART ================================

Enter a set of numbers (integers or floats) separated by comma:1,2,3,a
1,2,3,a

================================ RESTART ================================

Enter a set of numbers (integers or floats) separated by comma:a,1,2,3
Error

================================ RESTART ================================

Enter a set of numbers (integers or floats) separated by comma:1234a
1234a

================================ RESTART ================================

Enter a set of numbers (integers or floats) separated by comma:asdf3
Error

Thank you in advance!

Have you talked about regular expressions in your class yet? If so, you are probably expected to be using those. Take a look at http://docs.python.org/2/howto/regex.html

If not, you are probably expected to make a placeholder variable as true, loop through the entire string checking each character and make your placeholder variable false if bad characters are detected. You shouldn't use the return statement until you've gone through the whole string. (after the loop)

Firstly, you don't want the loop to return True until the end, but you want it to return False and break if the test is false. Your loop returns True or False right on the first iteration, so you don't want the two return statements within the loop. Secondly, you can create a list or string of characters and use it to check whether each character is within that list. In this case, it would be something like ",. " or [",", ".", " "] .

You should write something like this:

def is_numeric(s):

    for char in s:
        if not char.isdigit() and char not in [" ", ".", ","]:
            return False

    return True

Here, if the character is not a digit, or the character is not in the list [" ", ".", ","] , the function returns False , which automatically ends the loop. If the loop completes without returning a value, then all the characters meet your criteria and the function goes to the next line and returns True .

Edit:

If you want to use a placeholder variable, set it to True and write the loop to set it to False if the criteria aren't met. If the loop doesn't modify it, the condition is true for the entire string, so it remains True .

def is_numeric(s):
    is_digit = True

    for char in s:
        if not char.isdigit() and char not in [" ", ".", ","]:
            is_digit = False
            break

    return is_digit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM