-2
print("hello")
mass_list=[]
volume_list=[]
x=0
while not x=="":
    x=input("Enter Mass: ")
    if x.isnumeric():
        m=float(x)
        mass_list.append(m)
while len(volume_list) < len(mass_list):
    x=input("Enter volume: ")
    if x.isnumeric():
        v=float(x)
        volume_list.append(v)
print(mass_list)
print(volume_list)
density=[]
for n in range (0,len(mass_list)):
    d=(mass_list[n])/(volume_list[n])
    density.append(d)
print(density)
avg_density=0
for n in density:
    avg_density = avg_density + n
avg_density=avg_density/len(density)
avg_density=format(avg_density,".2f")
print(avg_density)

this code take two lists from the user and i want for the program to accept floats

I want the user to input 10 15 20 "" 1 1.5 2 and the output to be 10.00

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • You can't use `isnumeric()` for floats because the decimal point fails it. – Mark Ransom Nov 15 '22 at 03:52
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 15 '22 at 12:14

1 Answers1

0

In Python it's recommended to use the EAFP principle - don't try to verify the input, just use it and let it fail if it's wrong. So instead of:

x=input("Enter Mass: ")
if x.isnumeric():
    m=float(x)
    mass_list.append(m)

Just do:

x=input("Enter Mass: ")
m=float(x)
mass_list.append(m)

If you need to detect an error, put the whole works in a try/except block.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622