0

I'm getting the user to input an integer. If they don't do that, I want to inform them of their error and ask them to provide a different input.

This is what I have so far:

while (float(dimension) % int(dimension)) > 0:
    prompt = "That number won't work. Please enter an integer between 3-9"
    prompt += "\n>>>"
    dimension = input(prompt)

Every time I run it, let's say to test it I enter the number 5.1. This is the error I get in response:

ValueError: invalid literal for int() with base 10: 5.1

this happens with any non-integer number I enter in. Not sure what I'm doing wrong.

viraptor
  • 33,322
  • 10
  • 107
  • 191

1 Answers1

1

EAFP (Easier to ask for forgiveness than permission):

dimension = None
while dimension is None:
    try:
        dimension = int(input(">>>"))
    except ValueError:
        print("That number won't work. Please enter an integer.")

You'd need additional logic for 3-9; I'd probably code it as if dimension < 3 or dimension > 9: raise ValueError() ish kind of thing, but some would frown on that style.

Amadan
  • 191,408
  • 23
  • 240
  • 301