-2

I'm working on a Python 3.4 coding assignment that needs an integer input from the user. I am having trouble figuring out how to make the program accept only integer values; for instance, if the user inputs a float (i.e. "9.5"), the program will output, "That's not an integer! Try again."

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Emily
  • 81
  • 1
  • 1
  • 9
  • I am having trouble figuring out what your question is – Bhargav Rao Feb 02 '15 at 10:49
  • 2
    It would be a good idea to post the code you have so far, and how you have tried to approach it. That way it's easier for people to point out where your problem might be. – James Waddington Feb 02 '15 at 10:49
  • Accepts where? From raw_input(), from function parameter, from external file? How should program react if non-integer value was provided? Please clarify. – myaut Feb 02 '15 at 10:51
  • 1
    Sorry, I realize how vague the question was--hopefully, the edit elucidates my problem a bit better. – Emily Feb 02 '15 at 11:09
  • Thanks, all, for helping me find the solution to this problem! – Emily Feb 02 '15 at 11:16
  • It's always a good idea to include a piece of your own code in your question, so that people can see what you've tried. Ideally, make a little program that just focuses on the thing you're having a problem with, so that we can run it for ourselves. If you do that you will be **much** less likely to get down-votes on your question. – PM 2Ring Feb 02 '15 at 11:31
  • @PM2Ring, alright, I've got that down for future reference. I thought that since this was a simple-type question to a bigger code, it wouldn't need much detailing within the question. Now I know! – Emily Feb 02 '15 at 11:35
  • Ideally, a simple description without code would be adequate in this case. Unfortunately, we get lots of people here who expect us to do their work for them. By showing your own code we know you've at least tried to solve your problem yourself & that you're not just here for a hand-out. Also, seeing your code gives us an idea of how advanced your Python knowledge is, which helps us know at what level we should aim our answers. – PM 2Ring Feb 02 '15 at 11:44

1 Answers1

-1

Simple, use raw_input to get the string input, then call .isdigit() on it to see if it is an int. If it is, cast it to an int, then check it is within the valid range. Stick it all in a while loop so it keeps being called until a valid number is input, and you're all set.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27