0

I am creating a list in which I take values and convert them to floats. If the user enters a character A-Z/a-z, however, I must change that value to 0.0 and specify the location where the value has been changed. This is where I'm having trouble. I'm not exactly sure how I would find the values and change them to 0.0 if they're not numeric. Here is my code so far:

def main():
    # Creating the list
    num_list = []
    val = input("Enter a number or 0 to stop: ") 

    while val != '0': 
        num_list += [val] 
        val = input("Enter a number or 0 to stop: ") 
    #The list before values are changed to floats    
    print("Before: ", num_list) 

    try: 
        if val.isdigit():
            newnumlist = [] 
            for val in list:
                newnumlist.append(float(val)) 
        print(newnumlist)
    except ValueError: 

main()

After my try statement, I keep getting a TypeError. Do I need to use a variable, such as i, to get the values to change to floats? And in my except body, do I need a variable as well? How would I find the alphabetical characters in my list in order to change them?

Thank you in advance.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Joshua
  • 1
  • 2
  • where would you like to store the location? would you be making the item something like `(0.0,index)` if `val` is not a number? – R Nar Nov 18 '15 at 22:33

2 Answers2

2

(1) change

for val in list:

to

for val in num_list:

(2) change

except ValueError:

to

except ValueError:
    pass

(or whatever you want the program to do in the event of a ValueError).

This will work:

try:
    newnumlist = []
    for val in num_list:
        if val.isdigit():
            newnumlist.append(float(val))
        else:
            newnumlist.append('0.0')
    print(newnumlist)
except ValueError:
    pass

However, I have a feeling that you are trying to learn about exceptions so try (pun intended) this:

newnumlist = []
for val in num_list:
    try:
        newnumlist.append(float(val))
    except ValueError:
        newnumlist.append('0.0')

print(newnumlist)

Thanks ekhumoro!

Riccati
  • 461
  • 4
  • 13
  • For the ValueError, though, how would I change the alphabetical characters to a float? I'd use the replace method, I assume, but how would I be able to determine which characters are not numeric? – Joshua Nov 18 '15 at 22:46
  • 1
    In the first example, the `try/except` is redundant, because `isdigit` protects against value errors. In the second example, the `try/except` should go *inside* the loop (i.e. it just replaces the `isdigit` check). – ekhumoro Nov 18 '15 at 23:00
-1

You cant use isdigit to test if a string is a float. You need to make it your self and then map your list with this function:

def parse(string):
    try:
        return float(string)
    except Exception:
        raise TypeError

old_list = ["3.2","2.1"]
new_list = [parse(i) for i in old_list]


one line (without try/except) :
new_list = list(map(float,old_list))
# or other style
new_list = [float(i) for i in old_list] # certainly faster

Its exactly the same (certainly slower):

new_list = []
for i in old_list:
    new_list += [float(i)] # or [parse(i)]