Python Forum

Full Version: If the argument is not int then make it int!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi !
I have to write a function named Countdown:
def countdown(n):
    while n > 0:
        print(n)
        n = n - 1
    print('Blastoff!')
Now if I enter a non-integer value and I want Python to change it to int value how should I get Python to do that?

I treid with this:
def countdown(n):
    if n not int(n):
       n=int(t) 
    while n > 0:
        print(n)
        n = n - 1
    print('Blastoff!')
What is wrong with this code?
Just set the parameter as countdown(int(n)) will do. You don't need if statement here.
I think you are trying to write:
if type(n) is not int:
    n = int(n)
But since it is valid to pass an integer to int() why not just do this?
def countdown(n):
    n = int(n)
    while n > 0:
        print(n)
        n = n - 1
    print('Blastoff!')
Or this
def countdown(n):
    for i in range(int(n), 0, -1):
        print(i)
    print('Blastoff!')