Automate the Boring Stuff with Python - The Collatz sequence project
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.
I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = (number//2)
print(nextNumber)
else:
nextNumber = (3*number+1)
print(nextNumber)
print('Type in an integer.')
integer=input()
if integer.isdigit() ==True:
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
while integer.isdigit() != True :
print('Please try again.')
integer=input()
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
print('Thank you!')
python collatz-sequence
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
4
down vote
favorite
I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.
I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = (number//2)
print(nextNumber)
else:
nextNumber = (3*number+1)
print(nextNumber)
print('Type in an integer.')
integer=input()
if integer.isdigit() ==True:
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
while integer.isdigit() != True :
print('Please try again.')
integer=input()
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
print('Thank you!')
python collatz-sequence
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.
I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = (number//2)
print(nextNumber)
else:
nextNumber = (3*number+1)
print(nextNumber)
print('Type in an integer.')
integer=input()
if integer.isdigit() ==True:
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
while integer.isdigit() != True :
print('Please try again.')
integer=input()
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
print('Thank you!')
python collatz-sequence
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.
I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = (number//2)
print(nextNumber)
else:
nextNumber = (3*number+1)
print(nextNumber)
print('Type in an integer.')
integer=input()
if integer.isdigit() ==True:
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
while integer.isdigit() != True :
print('Please try again.')
integer=input()
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)
print('Thank you!')
python collatz-sequence
python collatz-sequence
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 49 mins ago


Mast
7,43063584
7,43063584
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 6 hours ago
Alex
211
211
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
One thing you’ll want to learn is not to repeat yourself, when coding.
Consider:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
print(nextNumber)
else:
nextNumber = 3*number+1
print(nextNumber)
You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
print(nextNumber)
You are printing in your collatz
generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.
Don’t use global
to return one value from a function. Just return the value,
def collatz(number):
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
return nextNumber
and let the caller assign it to whatever variable they want.
nextNumber = collatz( int(number) )
print(nextNumber)
while nextNumber != 1:
nextNumber = collatz(nextNumber)
print(nextNumber)
Reordering, to remove one of the calls to collatz
:
number = int(number)
while number != 1:
number = collatz( number )
print(number)
Python’s exception handling is exceptional! Learn it. Rely on it:
number = None
while number is None:
try:
number = int( input("Enter a number") )
except ValueError:
print("Try again")
# ... print collatz sequence here
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
One thing you’ll want to learn is not to repeat yourself, when coding.
Consider:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
print(nextNumber)
else:
nextNumber = 3*number+1
print(nextNumber)
You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
print(nextNumber)
You are printing in your collatz
generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.
Don’t use global
to return one value from a function. Just return the value,
def collatz(number):
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
return nextNumber
and let the caller assign it to whatever variable they want.
nextNumber = collatz( int(number) )
print(nextNumber)
while nextNumber != 1:
nextNumber = collatz(nextNumber)
print(nextNumber)
Reordering, to remove one of the calls to collatz
:
number = int(number)
while number != 1:
number = collatz( number )
print(number)
Python’s exception handling is exceptional! Learn it. Rely on it:
number = None
while number is None:
try:
number = int( input("Enter a number") )
except ValueError:
print("Try again")
# ... print collatz sequence here
add a comment |Â
up vote
3
down vote
One thing you’ll want to learn is not to repeat yourself, when coding.
Consider:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
print(nextNumber)
else:
nextNumber = 3*number+1
print(nextNumber)
You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
print(nextNumber)
You are printing in your collatz
generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.
Don’t use global
to return one value from a function. Just return the value,
def collatz(number):
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
return nextNumber
and let the caller assign it to whatever variable they want.
nextNumber = collatz( int(number) )
print(nextNumber)
while nextNumber != 1:
nextNumber = collatz(nextNumber)
print(nextNumber)
Reordering, to remove one of the calls to collatz
:
number = int(number)
while number != 1:
number = collatz( number )
print(number)
Python’s exception handling is exceptional! Learn it. Rely on it:
number = None
while number is None:
try:
number = int( input("Enter a number") )
except ValueError:
print("Try again")
# ... print collatz sequence here
add a comment |Â
up vote
3
down vote
up vote
3
down vote
One thing you’ll want to learn is not to repeat yourself, when coding.
Consider:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
print(nextNumber)
else:
nextNumber = 3*number+1
print(nextNumber)
You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
print(nextNumber)
You are printing in your collatz
generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.
Don’t use global
to return one value from a function. Just return the value,
def collatz(number):
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
return nextNumber
and let the caller assign it to whatever variable they want.
nextNumber = collatz( int(number) )
print(nextNumber)
while nextNumber != 1:
nextNumber = collatz(nextNumber)
print(nextNumber)
Reordering, to remove one of the calls to collatz
:
number = int(number)
while number != 1:
number = collatz( number )
print(number)
Python’s exception handling is exceptional! Learn it. Rely on it:
number = None
while number is None:
try:
number = int( input("Enter a number") )
except ValueError:
print("Try again")
# ... print collatz sequence here
One thing you’ll want to learn is not to repeat yourself, when coding.
Consider:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
print(nextNumber)
else:
nextNumber = 3*number+1
print(nextNumber)
You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:
def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
print(nextNumber)
You are printing in your collatz
generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.
Don’t use global
to return one value from a function. Just return the value,
def collatz(number):
if number % 2 == 0:
nextNumber = number//2
else:
nextNumber = 3*number+1
return nextNumber
and let the caller assign it to whatever variable they want.
nextNumber = collatz( int(number) )
print(nextNumber)
while nextNumber != 1:
nextNumber = collatz(nextNumber)
print(nextNumber)
Reordering, to remove one of the calls to collatz
:
number = int(number)
while number != 1:
number = collatz( number )
print(number)
Python’s exception handling is exceptional! Learn it. Rely on it:
number = None
while number is None:
try:
number = int( input("Enter a number") )
except ValueError:
print("Try again")
# ... print collatz sequence here
edited 46 mins ago


Mast
7,43063584
7,43063584
answered 3 hours ago
AJNeufeld
2,764313
2,764313
add a comment |Â
add a comment |Â
Alex is a new contributor. Be nice, and check out our Code of Conduct.
Alex is a new contributor. Be nice, and check out our Code of Conduct.
Alex is a new contributor. Be nice, and check out our Code of Conduct.
Alex is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205785%2fautomate-the-boring-stuff-with-python-the-collatz-sequence-project%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password