新程序员卡在 elif 上
New programmer stuck on elif
def computepay(h,r):
if h <= 40 :
p = r * h
print p
elif hrs >= 40 :
p = r * 40 + (r * 1.5 * (h - 40) )
print p
else :
Print "Error, you were payed to much"""
hrs = float(raw_input("Enter Hours:"))
# int can only represent whole numbers
rate = int(raw_input("Enter Rate:"))
# float can only represent floating-point values, that is, values that have a potential decimal place.
#r = float(rate)
p = computepay(h, r)
print "Pay",p
我正在尝试学习 python,但无法弄清楚为什么 computepay(h, r)
函数上的 elif
出现错误。
当您初始化函数时 p = computepay(h,r)
您传递的是不存在的值 h
和 r
(它们只是函数的签名),您需要传递 hrs
和 rate
喜欢:p = computepay(hrs,rate)
您的 elif
中有一个小的拼写错误,您键入了您打算使用的变量,而不是您定义为函数签名的变量 (h
)。
在您的函数定义中,您可以使用 h
,当您将其作为参数传递时,它将被替换为 hrs
。
所以您的 elif
将是:
elif h >= 40
你的比较有歧义:
if h <= 40 :
看看=
elif h >= 40 :
编辑:实际上它会转到您的 if
但会忽略您的 elif
因为它已经匹配了。
您的 else
块中也有错字,应该是 print
而不是 Print
并且您的末尾还有三个 "
字符串.
这没有给我任何错误:
def computepay(h,r):
if h <= 40 :
p = r * h
return p
elif h > 40 :
p = r * 40 + (r * 1.5 * (h - 40) )
return p
else :
return "Error, you were payed to much"
h = float(raw_input("Enter Hours:"))
# int can only represent whole numbers
r = int(raw_input("Enter Rate:"))
# float can only represent floating-point values, that is, values that have a potential decimal place.
#r = float(rate)
p = computepay(h, r)
print "Pay",p
你没有这样设置 p = computepay(hrs, rate)
,h
和 r
没有定义,所以即使你解决了你的识别错误,在你修复它之前它也不会起作用。你应该 return 值 p
,这就是为什么 print "Pay",p
的输出是 Pay None
def computepay(h,r):
if h <= 40 :
p = r * h
print p
elif hrs >= 40 :
p = r * 40 + (r * 1.5 * (h - 40) )
print p
else :
Print "Error, you were payed to much"""
hrs = float(raw_input("Enter Hours:"))
# int can only represent whole numbers
rate = int(raw_input("Enter Rate:"))
# float can only represent floating-point values, that is, values that have a potential decimal place.
#r = float(rate)
p = computepay(h, r)
print "Pay",p
我正在尝试学习 python,但无法弄清楚为什么 computepay(h, r)
函数上的 elif
出现错误。
当您初始化函数时 p = computepay(h,r)
您传递的是不存在的值 h
和 r
(它们只是函数的签名),您需要传递 hrs
和 rate
喜欢:p = computepay(hrs,rate)
您的 elif
中有一个小的拼写错误,您键入了您打算使用的变量,而不是您定义为函数签名的变量 (h
)。
在您的函数定义中,您可以使用 h
,当您将其作为参数传递时,它将被替换为 hrs
。
所以您的 elif
将是:
elif h >= 40
你的比较有歧义:
if h <= 40 :
看看=
elif h >= 40 :
编辑:实际上它会转到您的 if
但会忽略您的 elif
因为它已经匹配了。
您的 else
块中也有错字,应该是 print
而不是 Print
并且您的末尾还有三个 "
字符串.
这没有给我任何错误:
def computepay(h,r):
if h <= 40 :
p = r * h
return p
elif h > 40 :
p = r * 40 + (r * 1.5 * (h - 40) )
return p
else :
return "Error, you were payed to much"
h = float(raw_input("Enter Hours:"))
# int can only represent whole numbers
r = int(raw_input("Enter Rate:"))
# float can only represent floating-point values, that is, values that have a potential decimal place.
#r = float(rate)
p = computepay(h, r)
print "Pay",p
你没有这样设置 p = computepay(hrs, rate)
,h
和 r
没有定义,所以即使你解决了你的识别错误,在你修复它之前它也不会起作用。你应该 return 值 p
,这就是为什么 print "Pay",p
的输出是 Pay None