7 Replies - 3756 Views - Last Post: 07 August 2012 - 01:28 AM Rate Topic: -----

#1 Saer_x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-July 12

Force input to be positive and my function isn't running!

Posted 31 July 2012 - 10:22 PM

Hello everyone. I'm new with python language and I have problem with a piece of code.
What I'm trying to accomplish is to ask the user for a number and then print a countdown from that number to zero.
This is what i got so far.

#Ask user for a number
n = raw_input ("Enter a number:")

# print countdown of n
def countdown (n):
    while n > 0:
        print n
        n = n-1
    if n < 0:
        print "Enter a Positive Number"  # in case user try to enter a negative number (It seems to work)
    else:
        n = raw_input("Enter a number:") # If the user input a negative number, a "Enter a number" question should be ask again. I'm not sure if this is the correct way to do it ( please advise for a better way)



- once i enter the number, the program doesn't print the countdown. Please let me know what am I doing wrong
- I'm not asking for you to fix this code for me, but to give some examples in a way i can resolve and improved my code
- Thanks in advanced for your time and help!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Force input to be positive and my function isn't running!

#2 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 04:14 PM

My suggestion would be to take out all the bells and whistles and make it work at the moset basic level. Make a simple countdown from 10 to 0 then have it say something like Hooray. Once you can do that the rest of it should be easy.
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 04:19 PM

Firstly, please :code:. I fixed your post for you.

Secondly, You defined the countdown method, but you never CALLED it. You have to call the method after reading it in. It might be easier to put your definitions in first:
# print countdown of n
def countdown (n):
    while n > 0:
        print n
        n = n-1
    if n < 0:
        print "Enter a Positive Number"  # in case user try to enter a negative number (It seems to work)
    else:
        n = raw_input("Enter a number:") 


#Ask user for a number
n = raw_input ("Enter a number:")

# Call method
countdown(n)


Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,710
  • Joined: 19-March 11

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 06:47 PM

Think about the logic a little. Not in terms of code, but just in terms of the order of operations.

As it stands now, you have:

Get a number from the user
do a countdown if there's any "down" to count.
if the number was negative, tell them to enter a positive number
exit.

Does this seem odd to you? How would you fix it? Think in English first, then think in code.


One issue with your code - raw_input() returns a string. You're going to have to convert that to an int if you want to count with it. The simplest way is
n = int(n)


but that assumes you trust the user - if they enter "three" isntead of 3 then it all goes kablooie.
Was This Post Helpful? 1
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 07:27 PM

I addressed this before the attack, but now I have to do it all over again.... what a bother.

You should have read the list of things NOT to put into the topic title (it's on the page where you wrote the post). We don't care that your a self proclaimed "newbie" and saying "pls help" doesn't make us go faster, it's just annoying. We know you want help because you came to DreamInCode. We know that it's a Python question because you're in the Python forum. Can you imagine what an annoying jumbled mess these posts would be if everyone made their topic title "Newbie On Python. Pls Help!"? I'll be renaming your topic to something appropriate.

Now that this has been settled. A couple suggestions,
1.) Building off of what jon said, there's actually a Python snippet called Get Input of A Specified Type that forces the user to input a specific type of data, for example an integer. You can check it out if you're interested.

2.) An algorithm to force the input to be positive is pretty easy and the techniques used to accomplish it should be pretty familiar to you. Here's the pseudo-code:
Ask user for input
while user input negative
    chide the user for his bad input
    ask the user for input


Was This Post Helpful? 1
  • +
  • -

#6 Saer_x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-July 12

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 08:20 PM

View PostDogstopper, on 01 August 2012 - 04:19 PM, said:

Firstly, please :code:. I fixed your post for you.

Secondly, You defined the countdown method, but you never CALLED it. You have to call the method after reading it in. It might be easier to put your definitions in first:
# print countdown of n
def countdown (n):
    while n > 0:
        print n
        n = n-1
    if n < 0:
        print "Enter a Positive Number"  # in case user try to enter a negative number (It seems to work)
    else:
        n = raw_input("Enter a number:") 


#Ask user for a number
n = raw_input ("Enter a number:")

# Call method
countdown(n)



Oh I see clear now. It makes sens. Also, thanks to everyone for your time and help. I really appreciated. Cheerss!!!
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Force input to be positive and my function isn't running!

Posted 01 August 2012 - 08:37 PM

I did forget to convert it to an integer. Make sure you do that.
Was This Post Helpful? 0
  • +
  • -

#8 Saer_x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-July 12

Re: Force input to be positive and my function isn't running!

Posted 07 August 2012 - 01:28 AM

Thanks everyone for helping me with this problem.
Every single advice was helpful. Cheers!!!

-Saer_x
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1