Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function problem
#1
'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")
Reply
#2
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)
Reply
#3
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 Undecided

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.
Reply
#4
I called it using:
birthexp(1990)
But all it did was move to the next line.
Reply
#5
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)
Output:
4: product after multiplying the birth year to itself. 0: number of ones found at a rate of 0.0percent. 0: number of threes found at a rate of 0.0percent 0: number of fives found at a rate of 0.0percent 0: number of sevens found at a rate of 0.0percent 0: number of nine found at a rate of 0.0percent 0: total odd numbers found at a rate of 0.0percent
Reply
#6
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.
Reply
#7
1990 isn't a big number. Lack of memory can't be an issue here
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.
Reply
#9
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
Reply
#10
(Jan-14-2017, 05:39 PM)wavic Wrote: 1990 isn't a big number. Lack of memory can't be an issue here
He 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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020