Python 2.7 为什么赢了';我的函数不能执行吗 我对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)

Python 2.7 为什么赢了';我的函数不能执行吗 我对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),python-2.7,Python 2.7,这一行有一个打字错误totalRatio=sumCount/floar(length)*100。您需要float而不是floar 其次,使用print函数,几乎所有行中都有大量缺少的括号 如果希望函数返回值,应使用return而不是print: return (str(product) + ": product after multiplying the birth year to itself.\n" + str(onesCount)

这一行有一个打字错误
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")

这是该程序中的所有代码吗?是否在某处调用此函数?
totalRatio=sumCount/floar(length)*100
。有一个输入错误:
floar
应该是
float
。函数在python中不会运行,除非在
birthexp(1990)
之类的地方调用它们。将运行
birthyear
等于1990的代码。即使修复了输入错误并调用了函数,它仍然对我没有任何帮助。它所做的只是缩进到下一行,而不是调用函数I修复了缺少的括号和浮点输入错误。然而,它仍然忽略了这个功能。撇开这一点不谈,我的函数中的逻辑是否正确?在调用函数后,它会转到下一行,就好像它返回了一样nothing@gadde2223-嗯,它什么也不回,它只是打印information@gadde2223-它不应该返回任何内容,因为函数中没有
return
语句。您指出,输出太大,导致它无法显示。也许我只是想得太多了。但是谢谢。