为什么在此代码中出现EOF错误?

时间:2020-08-01 00:18:42

标签: python python-3.x

def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75

cost = (weight*per)+20
return print("Price = $"+str(cost))

x=input("Enter the weight")
ground_ship(x)

2 个答案:

答案 0 :(得分:1)

该错误是由于倒数第二行引起的-您需要将用户输入更改为浮点数:

def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75
      
   cost = (weight*per)+20
    
   return print("Price = $"+str(cost))

x=float(input("Enter the weight"))
ground_ship(x)

答案 1 :(得分:0)

在您提供的代码中,您有两行缩进错误。 为了使您的代码正常工作,您需要在执行if语句之前将输入转换为整数。

这是解决您问题的有效方法。

def ground_ship(weight):
   if weight <= 2:
      per = 1.50
   elif weight > 2 and weight <= 6:
      per = 3.00
   elif weight > 6 and weight <= 10:
      per = 4.00
   else:
      per = 4.75

   cost = (weight*per)+20
   return print("Price = $"+str(cost))


x=int(input("Enter the weight: "))
ground_ship(x)

请注意,如果您放置一个字符串,它将破坏您的程序,我建议您在尝试对要读取的值进行任何假设之前,使用try catch进行一些错误处理。