Search In
• More options...
Find results that contain...
Find results in...

# Need some quick python help

I have most of the script.. I poached it from somewhere else.. I am no programmer, though I do want to learn..

That being said, I am trying to make this script auto increase the user input until it finds a number that does not end in "1" (Collatz Conjecture).  Is this something that's easily done?  Can it also only show the number of steps instead of the work output?

Thanks

```def collatz3(inputy):

listy = [inputy]

while inputy != 1:
if inputy % 2 == 0:
inputy = inputy // 2
else:
inputy = 3 * inputy + 1

listy.append(inputy)

return listy

userInput = input('Type in a number: ')

while True:
try:
userInput = int(userInput)
break
except ValueError:
userInput = input('That is not a number. Type in a number: ')

collatzSeq = collatz3(userInput)
print(collatzSeq)```

##### Share on other sites

Is this what you need?

The code will return the next number that does not end in 1 and the steps in a list ['number','steps']

EDIT: code has a few bugs will sort them out soon if you still need it

EDIT2: FIXED

```def nextInt(input1):
try:
integer = int(input1)

except:
return 'ERROR'

steps = 0

if integer > 1 and integer < 10:
value = [integer,steps]
return value

elif integer == 1:
integer = 2
steps += 1
value = [integer,steps]
return value
else:
while True:
if integer % 10 != 1:
value = [integer,steps]
return value
else:
integer += 1

selectedNum = input('Enter a number: ')
print(nextInt(selectedNum))

```

##### Share on other sites

9 minutes ago, Bitzs said:

Is this what you need?

The code will return the next number that does not end in 1 and the steps in a list ['number','steps']

EDIT: code has a few bugs will sort them out soon if you still need it

EDIT2: FIXED

This doesnt seem to be working.. I was hoping to be able to say enter "5" and have it spit out the number of steps and automatically move on to "6", using the following mathematical portion of the original code:

```while inputy != 1:
if inputy % 2 == 0:
inputy = inputy // 2
else:
inputy = 3 * inputy + 1```

Thanks for the help!

##### Share on other sites

9 hours ago, SVThuh said:

This doesnt seem to be working.. I was hoping to be able to say enter "5" and have it spit out the number of steps and automatically move on to "6", using the following mathematical portion of the original code:

```while inputy != 1:
if inputy % 2 == 0:
inputy = inputy // 2
else:
inputy = 3 * inputy + 1```

Thanks for the help!

Ok I've edited it to use your calculation, the print statement will return [the number its on, the number of steps, the final value] The code will also stop when the return value is not 1. In addition I added time.sleep to slow it down between calculations to make the data readable but if you want to speed it up you can just remove that line

```import time

def nextInt(input1):
cont = True

try:
input1 = int(input1)

except:
return 'ERROR'

while cont:

steps = 0

input2 = input1
while input2 != 1:
steps += 1
if input2 % 2 == 0:
input2 = input2 // 2
else:
input2 = 3 * input2 + 1

value = [input1,steps, input2]
print(value)

time.sleep(0.1)
if value[2] != 1:
cont = False
else:
input1 += 1

nextInt(input('Input a integer: '))```

##### Share on other sites

This is EXACTLY what I needed.. Thank you.. Putting my old twin Xeon machine to work with this right now.  Works a treat!

##### Share on other sites

16 hours ago, SVThuh said:

This is EXACTLY what I needed.. Thank you.. Putting my old twin Xeon machine to work with this right now.  Works a treat!

Glad it works, happy to help