python TypeError:接受1个位置参数,但提供了2个

4nkexdtk  于 2022-11-27  发布在  Python
关注(0)|答案(4)|浏览(298)

编写一个函数,以便下面的主程序可以被调用函数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

pzfprimi

pzfprimi1#

添加此函数而不是您在问题中定义的函数,应该可以修复错误。

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

因此,总代码如下所示:

def mph_and_minutes_to_miles(miles_per_hour, minuted_traveled):
    hours_traveled = minutes_traveled / 60
    miles = hours_traveled * 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))
r55awzrz

r55awzrz2#

这里有两个选项,只需替换以下选项中的一个即可

def mph_and_minutes_to_miles(miles_per_hour, minutes_traveled):

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

ryhaxcpt3#

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)))
2g32fytz

2g32fytz4#

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))

相关问题