Can't convert a number to a float

末鹿安然 提交于 2019-12-24 20:05:16

问题


I'm trying to solve a homework problem as an extreme novice to python, and I don't understand what I'm doing wrong. I have the code and error message below, hoping that someone can tell me where I went wrong.

The miles input tries 10, 50, then 100. Which works. Mpg is 20.0. Also works. gp input is 3.1599, which gives me this error. Not sure why.

miles = float(input())  
mpg = float(input())
gp = float(input())
price = (miles*1.0/mpg)*gp
print(price)

Traceback (most recent call last): File "main.py", line 3, in gp = float(input()) EOFError: EOF when reading a line


回答1:


Works fine for me. See below:

Output




回答2:


EOF error results usually in an online compiler when an input is given by the system but the input reading line is not at the right place to read it. Read the sample input syntax again and try. or give input inside code.

you can use try catch block for solve this problem please check below code:

in your code you did not took miles as int in line 1 but you try to multiply it with float in line 6.

try:
  miles = float(input())   
  print('Enter miles per gallon:')
  mpg = float(input())
  print('Enter the gas price:')
  gp = float(input())
  price = (miles*1.0/mpg)*gp  
  print(price)
except:
  pass



回答3:


`mpg = float(input('Enter miles per gallon:'))
 gp = float(input('Enter the gas price:'))
 price = (10*1.0/mpg)*gp print(price) 
 price = (50*1.0/mpg)*gp print(price) 
 price = (400*1.0/mpg)*gp print(price)`      

This was the answer, solved



来源:https://stackoverflow.com/questions/58105356/cant-convert-a-number-to-a-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!