Function problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Function problem (/thread-1594.html) |
Function problem - garretmorion - Jan-14-2017 'm relatively new to python and the only other experience I've had is C++. Whenever I define a function in Python, I can't seem to execute it. This my current code for my assignment, if possible I just want to know why my code won't execute 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") RE: Function problem - buran - Jan-14-2017 That is because you never call the function # function definition def foo(): print 'function foo' #another function definition def power2(x): return x**2 # function call foo() #another call n = power2(3) print n #yet another call print power2(5) RE: Function problem - snippsat - Jan-14-2017 You call function like this,there is one argument birthyear. birthyear = 1980 birthexp(birthyear)There are several syntax error in your code. Edit: Which you have fixed now print look better if you use string formatting. Eg: >>> onescount = 20 >>> oneRation = 100 >>> print('{}: number of ones found at a rate of {} percent.'.format(onescount, oneRation)) 20: number of ones found at a rate of 100 percent. RE: Function problem - garretmorion - Jan-14-2017 I called it using: birthexp(1990) But all it did was move to the next line. RE: Function problem - snippsat - Jan-14-2017 Here your code now with function call. And i call with a smaller number,try this 1980 ** 1980 get really big. 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") birthexp(2)
RE: Function problem - garretmorion - Jan-14-2017 So, the problem was that I was using a number that was too big? Thank you for pointing that out for me. I'm so used to getting a memory error when I did this with another language that it never occurred to me that that was the reason. Again, thank you. RE: Function problem - wavic - Jan-14-2017 1990 isn't a big number. Lack of memory can't be an issue here RE: Function problem - garretmorion - Jan-14-2017 I've finally made it work. There was a problem during the startup of my interpreter which was the cause of my all my problems. Thank you for all the help though. RE: Function problem - buran - Jan-14-2017 no, it's not a memory problem. you did something else. There is no problem to calculate 1980**1980, just the output is too long also note that ** is power operator, not multiplication RE: Function problem - snippsat - Jan-14-2017 (Jan-14-2017, 05:39 PM)wavic Wrote: 1990 isn't a big number. Lack of memory can't be an issue hereHe has in code 1990 ** 1990 ,so then it get bigger.Should not be a memory problem with that power operator, but it look a little strange with that big number then comes rest of the results. |