-3
sides=int(input("Please enter the amount of sides on the dice: "))
times=int(input("Please enter the number of times you want to repeat: "))

I want to repeat those two lines until the user input an int number for each request.
Thanks

user3358812
  • 21
  • 1
  • 2
  • A good place to start would be to learn about [while loops](https://wiki.python.org/moin/WhileLoop). Then, take a peek at http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except – Blue Ice Mar 05 '14 at 00:46
  • 1
    Take a look at some tutorials and [documentation](http://docs.python.org/2/tutorial/controlflow.html#for-statements), and report back when you get stuck. You won't really get the most out of Stack Overflow until you've learned the basic foundations of the language. – mhlester Mar 05 '14 at 00:46
  • something similar to do-while loop – user3358812 Mar 05 '14 at 00:54

2 Answers2

2

Reading loosely,

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:    # wasn't an int
            pass

sides = get_int("Please enter the amount of sides on the dice: ")
times = get_int("Please enter the number of times you want to repeat: ")

but if you strictly want to repeat both statements until both get an int value,

while True:
    s1 = input("Please enter the amount of sides on the dice: ")
    s2 = input("Please enter the number of times you want to repeat: ")
    try:
        sides = int(s1)
        times = int(s2)
        break
    except ValueError:
        pass
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0
sides=0;times=0
while(sides == 0 and times == 0):
    sides=int(input("Please enter the amount of sides on the dice: "))
    times=int(input("Please enter the number of times you want to repeat: "))
eptgrant
  • 119
  • 7
  • It return error and I have to reload the code again. I want to repeat those two lines until the user input an int number for each request. – user3358812 Mar 05 '14 at 00:52
  • 1
    it's returning an error because you're telling the code to `int()` whatever the user gives it. What happens if it's not a number? Python will throw an error. Read about [`try/catch`](http://docs.python.org/3.2/tutorial/errors.html) statements to catch that error and continue with execution – Adam Smith Mar 05 '14 at 00:54