0

I am working on a collatz sequence code in python. The code should give me a sequence of numbers that end in 1. The code I have here does that when I enter a number.

    try:
    number = int(input('Pick a number'))
except ValueError:
    print('Error! input a number')
def collatz(number):
            if number % 2 == 0:
                x = number // 2
                return x
            else:
                x = 3 * number + 1
                return x

while number != 1:
        number = collatz(number)
        print(number)

However, when I try to invoke the try and except function by entering a letter,I get the desired error message but I also get a NameError.

Traceback (most recent call last):
  File "/home/PycharmProjects/collatz/collatz.py", line 14, in <module>
    while number != 1:
NameError: name 'number' is not defined
Error! input a number *Desired Error Message*

I dont get this error when I remove the try and except function. I have tried defining 'name' as a global variable and also played around with the indentation but nothing seems to work. I would greatly appreciate any kind of help.

I am using python 3.6.

JM9
  • 119
  • 1
  • 8
  • This doesn't directly answer your question, but [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) may be of interest to you – Kevin May 24 '19 at 13:44
  • your problem is not variable `name` but variable `number`. When you enter letter then you get error and code in `try` doesn't create variable `number` - so later you have error `"number" is not defined` for line `while number != 1:`. You have to define `number` with some default value inside `except` or before `try` – furas May 24 '19 at 14:01
  • @furas I understand what you are saying but I am not exactly sure how to implement it into a code. If its not too much trouble, can you show me how you would do that? – JM9 May 24 '19 at 15:08
  • there is nothing to show. Simply use `number = 1` before `try` and it will create variable `number` with default value `1` - so `number` will exist even when you enter letter. And this resolve problem with `'number' is not defined`. – furas May 24 '19 at 18:22
  • Thank you @furas That solved the issue. – JM9 May 27 '19 at 16:09

2 Answers2

0

The reason you get the NameError is because number is simply not defined. The call to int fails, thus the assignment to number never happens, you catch and print the error but then just proceed. To force the user to enter a valid number you have to repeat the prompt until the user enters the correct input.

def read_number():
    while True:
        try:
            return int(input('Pick a number'))
        except ValueError:
            print('Error! Input a number')

Then, to read a sequence of numbers you do:

while True:
    number = read_number()
    if number == 1:
        break
Kris
  • 1,358
  • 13
  • 24
0

If your ValueError get raised, then I think you don't really define number in any other place. That's why your while loop raises a NameError.

lpozo
  • 598
  • 2
  • 9