开发者

Python - Running a function but the number isnt decreased when told to

I have a function which defines a number, than determines what the last digit is, and than does a formula based on the last number, UNTIL the number reaches '1'

YES this is project Euler Problem 14

after the function runs, the original number is to be decreased by 1 and then the function is to be run again.

i already realise how painfully bad this method is but once i know exactly how to get the correct answer, i will make the code alot sexier and quicker to find the answer.

my code:

def get_count():
    number = 1000000
    value = number
    count = 0
    while value != 1:
        if value % 10 == 0:
            count += 1
            value *= 0.5
        elif value % 10 == 2:
            count += 1
            value *= 0.5
        elif value % 10 == 4:
            count += 1
            value *= 0.5
        elif value % 10 == 6:
            count += 1
            value *= 0.5
        elif value % 10 == 8:
            count += 1
            value *= 0.5
        elif value % 10 == 1:
            value = 3*value + 1
            count += 1
        elif value % 10 == 3:
            value = 3*value + 1
            count += 1
        elif value % 10 == 5:
            value = 3*value + 1
            count += 1
        elif value % 10 == 7:
            value开发者_JAVA技巧 = 3*value + 1
            count += 1
        elif value % 10 == 9:
            value = 3*value + 1
            count += 1
    print(">>", count, "<<")
    number -= 1
get_count()
get_count()

But when i run this it gives me the same answer twice, instead of minusing 1 it stays the same. am i missing something here? I am working back from 1Million as i figured the higher "stepcount" would be a higher number, but i will eventually be proven correct or wrong. James EDIT: I have solved my issue, the code for it is:

number = 1000000
def get_count():
    value = number
    count = 0
    while value != 1:
        if value % 10 == 0:
            count += 1
            value *= 0.5
        elif value % 10 == 2:
            count += 1
            value *= 0.5
        elif value % 10 == 4:
            count += 1
            value *= 0.5
        elif value % 10 == 6:
            count += 1
            value *= 0.5
        elif value % 10 == 8:
            count += 1
            value *= 0.5
        elif value % 10 == 1:
            value = 3*value + 1
            count += 1
        elif value % 10 == 3:
            value = 3*value + 1
            count += 1
        elif value % 10 == 5:
            value = 3*value + 1
            count += 1
        elif value % 10 == 7:
            value = 3*value + 1
            count += 1
        elif value % 10 == 9:
            value = 3*value + 1
            count += 1
    print(">>", number, ":", count, "<<")
num = 1000000
while num > 1:
    num -= 1
    get_count()
    number -= 1

Although this is seriously, seriously bad, and super slow to run, it does what i neeeded it to, now i shall be off, to find a way to make it faster, and find a way to give me the direct answer.


Local variables don't exist outside of their enclosing function. Every time you call this function you're getting a whole new variable number, value and count and your assignments are all run again.

If you want variables that outlive your function two options are to make them globals or to make the function a method of a class, and have the variables be instance variables of that class.

Globals approach:

number = 1000000
value = number
count = 0

def get_count():
    global number, value, count
    while value != 1:
        if value % 10 == 0:
    # etc.

Class approach:

class Foo(object):
    def __init__(self):
        self.number = 1000000
        self.value = number
        self.count = 0

    def get_count():
        while self.value != 1:
            if self.value % 10 == 0:
        # etc.

Note that in the class approach you need to use self. to access the instance variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜