I am trying to build a simple program that runs the Collatz Sequence on a number inputted by the user. I am also using this to try and get the hang of using 'try except' statements in Python by throwing a print statement whenever the user inputs a string rather than an integer.
The program works as expected when entering a integer, for example here I input the number 12 and I get the following output:
$ python collatzSequence.py
Enter in a number:
12
6
3
10
5
16
8
4
2
1
However, when you input a string I get this error:
python collatzSequence.py
Enter in a number:
ddd
Traceback (most recent call last):
File "collatzSequence.py", line 34, in <module>
main()
File "collatzSequence.py", line 25, in main
numb = input()
File "<string>", line 1, in <module>
NameError: name 'ddd' is not defined
When what I want to be able to do is catch when the user enters something that is not an integer and print 'Please enter an integer.'
The following is my code that I have written, I have placed the 'try except' statement inside the collatz() function, I had also tried putting it in the main() function but I got the same result. :
# Exploring the 'Collatz Sequence'.
# If number is even, print number // 2 and returns this value.
# If number is odd, print and return 3 * number + 1.
def collatz( number ) :
# Error handling to make sure number entered is an int.
try:
number = int(number)
except NameError :
print( 'Please enter an integer.')
return
# number is even.
if number % 2 == 0 :
print( str(number // 2) )
return number // 2
# number is odd.
else :
print( str(3 * number + 1) )
return 3 * number + 1
# Lets user type in an integer and keeps calling collatz() on that number until the funciton returns the value 1.
def main() :
print( "Enter in a number: " )
numb = input()
numb = collatz(numb)
while numb != 1 :
numb = collatz(numb)
if __name__ == "__main__":
main()
Thanks for any help anyone can give me with working out what I can do to fix this issue.
Thanks to DyZ for the solution to this. The problem was that I had the 'try except' statement in the wrong spot. I needed to have the 'numb = input()' line in the try statement in the main() function as that is the line that raises the NameError. I also added a while statement so it would continue to ask for an integer if the user keeps inputting a string. This loop breaks once an int is entered. The following is the fixed code :
# Exploring the 'Collatz Sequence'.
# If number is even, print number // 2 and returns this value.
# If number is odd, print and return 3 * number + 1.
def collatz( number ) :
number = int(number)
# number is even.
if number % 2 == 0 :
print( str(number // 2) )
return number // 2
# number is odd.
else :
print( str(3 * number + 1) )
return 3 * number + 1
# Lets user type in an integer and keeps calling collatz() on that number until the funciton returns the value 1.
def main() :
print( "Enter in a number: " )
# Error handling to make sure number entered is an int.
while True :
try:
numb = input()
break
except NameError :
print( 'Please enter an integer.' )
numb = collatz(numb)
while numb != 1 :
numb = collatz(numb)
if __name__ == "__main__":
main()
Giving the result:
$ python collatzSequence.py
Enter in a number:
ddd
Please enter an integer.
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Thanks again DyZ!