Python Forum
If the argument is not int then make it int!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If the argument is not int then make it int!
#1
Lightbulb 
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?
Reply
#2
Just set the parameter as countdown(int(n)) will do. You don't need if statement here.
pooyan89 likes this post
Reply
#3
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!')
pooyan89 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating functions - make optional argument a reversal anstuteville 2 2,628 Aug-31-2018, 02:36 PM
Last Post: anstuteville

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020