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.