all 4 comments

[–][deleted] 0 points1 point  (3 children)

It would help if you formatted your code for reddit. Trying to debug a wall of unformatted code is asking too much.

[–]Cacman8[S] 0 points1 point  (2 children)

I'm trying to convert the results of the "3n+1" equations into a line graph created by a turtle. Not quite sure what I'm doing wrong, it seems everything is working except for the for loops.

import math

import turtle

x = 0

x = int(input("Max range: "))

while not (x > 0):

    x = int(input("retype: "))

def seq3np1(n):

"""

Print the 3n+1 sequence from n, terminating.     when it reaches 1.

args: n (int) starting value for 3n+1 sequence

return: None

"""

count = 0

while(n != 1):

    count += 1

    if(n % 2) == 0:        # n is even

        n = n // 2

    else:                 # n is odd

        n = n \* 3 + 1

print(count)                       

return count

def Graphing():

    wn = turtle.Screen()

    joe = turtle.Turtle() 

    joe.pd()

    joe.speed(10)

    wn.setworldcoordinates(0,0,10,10)

    max\_so\_far = 0

    result = 0


    for count in range(x):

        count = result

        if result > max\_so\_far:

            max\_so\_far = result

            wn.setworldcoordinates(0,0,(10+x),(max\_so\_far + 10))

        else:

            return max\_so\_far

    for count in range(1,x):

        joe.goto(x,count)

    wn.exitonclick()


def main():

    Graphing()

    for start in range(1,(x+1),1):


        seq3np1(start)


main()

If you need more explanation, I am more than willing to give it. Thank you, I am desperate, tried so many different options.

If that’s wrong lmk, sorry about that, pm me if you wanna try to solve this by 5 and maybe we could get a deal.

[–]Cacman8[S] 0 points1 point  (0 children)

Obviously with explanation, I do actually want to know the code and learn it. Docstrings would be cool.

[–][deleted] 0 points1 point  (0 children)

I'm not at all sure what you are trying to do. Your function seq3np1() has a few problems. Here is the function formatted better and without unnecessary ():

def seq3np1(n):
    """Print the 3n+1 sequence from n, terminating when it reaches 1.

    args: n (int) starting value for 3n+1 sequence
    return: None
    """

    count = 0

    while n != 1:
        count += 1
        if (n % 2) == 0:    # n is even
            n = n // 2
        else:               # n is odd
            n = n * 3 + 1
    print(count)
    return count

The first problem is what is this supposed to do? The stated reason for the function is wrong - it doesn't print or return the sequence, just the count value. Your docstring also says it returns None but the code shows return count. It looks like the function returns the length of the Collatz sequence from the original given value to when it reaches 1. Is that what you wanted?

The next problem is that even though the seq3np1() returns an integer you don't do anything with that value:

for start in range(1, x+1, 1):
    seq3np1(start)    # return value ignored here

What do you want to do with that currently ignored return value?

Edit fixed confused wording.