0

Is the statement if how_many == int: not valid? The code after that block is not executing.

from random import randint

print('Code Generator')
print()

def generator():
    how_many = int(input('How many digits? '))
    print('Code is: ', end='')
    if how_many == int:
        for number in range(how_many):
        number = randint(0, 9)
        print(number, end=' ')
   else:
        print('Enter a number')
        generator()

   print()
   again = input('Again?(Y/N): ')
   if again.upper() == 'Y':
       generator()
   else:
       print('Goodbye!')
       exit()

generator()
khelwood
  • 55,782
  • 14
  • 81
  • 108
nasb
  • 1
  • 1
  • 2
    `isinstance(how_many, int)`. But since you cast `how_many` to int, it definitely will be an int, so the `if` check is redundant. – khelwood Apr 23 '20 at 09:44
  • `int(input('How many digits? '))` casts your input as an int so this line `if how_many == int:` will never do anything useful. `int()` will fail if you pass it something that it cant parse as a number, also to check type you can do what @khelwood suggested or `type(how_many) is int`. also also, your else is a bad idea, why use recursion here? – Nullman Apr 23 '20 at 09:53
  • Thanks for your input. What I want to do here is: if the input is not a number, the else block will execute until the user enters a number. Is there other way to do that? Thanks – nasb Apr 23 '20 at 10:16

0 Answers0