TypeError:采用1个位置参数,但给出了2个

时间:2019-07-05 21:22:11

标签: python

编写一个函数,以便下面的主程序可以被调用mph_and_minutes_to_miles()函数的更简单的代码替换。原始主程序:

miles_per_hour = float(input())
minutes_traveled = float(input())
hours_traveled = minutes_traveled / 60.0
miles_traveled = hours_traveled * miles_per_hour

print('Miles: %f' % miles_traveled)

那是问题。

def mph_and_minutes_to_miles(miles_per_hour, minutes_traveled):
    minutes_traveled = float(input())
    hours_traveled = minutes_traveled / 60
    miles = (minutes_traveled / 60) * miles_per_hour

miles_per_hour = float(input())
minutes_traveled = float(input())

print('Miles: %f' % mph_and_minutes_to_miles(miles_per_hour, minutes_traveled))

TypeError:必须为实数,而不是NoneType

4 个答案:

答案 0 :(得分:1)

您在这里有两个选择,只需替换以下其中一个

def mph_and_minutes_to_miles(miles_per_hour, minutes_traveled):

print('Miles: %f' % mph_and_minutes_to_miles(miles_per_hour))

答案 1 :(得分:1)

添加此功能而不是您在问题中定义的功能,应该可以解决您的错误。

def mph_and_minutes_to_miles(miles_per_hour, minuted_traveled):
    hours_traveled = minutes_traveled / 60
    miles = (minutes_traveled / 60) * miles_per_hour
    return miles

所以总代码如下:

def mph_and_minutes_to_miles(miles_per_hour, minuted_traveled):
    hours_traveled = minutes_traveled / 60
    miles = (minutes_traveled / 60) * miles_per_hour
    return miles

miles_per_hour = float(input())
minutes_traveled = float(input())

print('Miles: %f' % mph_and_minutes_to_miles(miles_per_hour, minutes_traveled))

答案 2 :(得分:0)

def mph_and_minutes_to_miles(英里/小时,minuted_traveled):

hours_traveled = minutes_traveled / 60
miles = (minutes_traveled / 60) * miles_per_hour
return miles

miles_per_hour =浮点数(input()) minutes_traveled = float(input())

print('Miles:%f'%mph_and_minutes_to_miles(miles_per_hour,minutes_traveled))

答案 3 :(得分:0)

def mph_and_minutes_to_miles(miles_per_hour, minutes_traveled):
    hours = minutes_traveled / 60.0
    miles = hours * miles_per_hour

    return miles

miles_per_hour = float(input())
minutes_traveled = float(input())

print('Miles: {:f}'.format(mph_and_minutes_to_miles(miles_per_hour, minutes_traveled)))