我对python相对较新,而我唯一的其他体验是C ++。每当我在Python中定义一个函数时,我似乎无法执行它。 这是我当前的作业代码,如果可能,我只想知道为什么我的代码不会执行
def birthexp(birthyear):
product = birthyear**birthyear
length = len(str(product))
onesCount = str(product).count("1")
threeCount = str(product).count("3")
fiveCount = str(product).count("5")
sevenCount = str(product).count("7")
nineCount = str(product).count("9")
sumCount = onesCount+threeCount+fiveCount+sevenCount+nineCount
oneRation = onesCount/float(length)*100
threeRatio = threeCount/float(length)*100
fiveRatio = fiveCount/float(length)*100
sevenRatio = sevenCount/float(length)*100
nineRatio = nineCount/float(length)*100
totalRatio = sumCount/float(length)*100
print(str(product) + ": product after multiplying the birth year to itself.")
print(str(onesCount) + ": number of ones found at a rate of " +str(oneRation)+ "percent.")
print(str(threeCount) + ": number of threes found at a rate of " +str(threeRatio)+ "percent")
print(str(fiveCount) + ": number of fives found at a rate of " +str(fiveRatio)+ "percent")
print(str(sevenCount) + ": number of sevens found at a rate of " +str(sevenRatio)+ "percent")
print(str(nineCount) + ": number of nine found at a rate of " +str(nineRatio)+ "percent")
print(str(sumCount) + ": total odd numbers found at a rate of " +str(totalRatio)+ "percent")
birthyear(1990)
答案 0 :(得分:0)
此行totalRatio = sumCount/floar(length)*100
中有拼写错误。您需要float
而不是floar
。
其次,几乎所有使用print
函数的行都有大量缺失的括号。
如果您希望该函数返回值,则应使用return
代替print
:
return (str(product)
+ ": product after multiplying the birth year to itself.\n"
+ str(onesCount)
+ ": number of ones found at a rate of " + str(oneRation) + "percent.\n"
+ str(threeCount)
+ ": number of threes found at a rate of " + str(threeRatio) + "percent\n"
+ str(fiveCount)
+ ": number of fives found at a rate of " + str(fiveRatio) + "percent\n"
+ str(sevenCount)
+ ": number of sevens found at a rate of " + str(sevenRatio) + "percent\n"
+ str(nineCount)
+ ": number of nine found at a rate of " + str(nineRatio) + "percent\n"
+ str(sumCount)
+ ": total odd numbers found at a rate of " + str(totalRatio) + "percent\n")