0

I'm trying to set up the below code to ask the user to enter two integers (num_sides & num_rolls). If the user doesn't enter an integer for either input, then the code should print the statement "Enter an integer".

I find this code only tests the num_sides but doesn't test if num_rolls is an integer. What's going on here?

Thanks in advance for your help!

def rolldice():
    while True:
        while True:
            num_sides = input("Enter number of sides for die: ")
            num_rolls = input("Enter number of rolls for die: ")
            try:
                if int(num_sides) != num_sides or int(num_rolls) != num_rolls:
                    break
                break
            except(ValueError):
                print("Enter an integer")
                True
4
  • 4
    What an odd loop. For starters, num_sides is being cast to an int and being compared to its original form (a string), which will never be True. Commented May 14, 2015 at 3:25
  • Why do you have breaks both inside and outside your if? And what did you expect True on a line by itself to do for you? Commented May 14, 2015 at 3:26
  • Some of the answers just test whether they can be cast to int. Are you also trying to check whether the numbers are integers, or is that not important? I mean, if someone puts in 3.3, do you want it to throw an exception (test whether the number is an integer) or just cast to 3 (which is what the current answers do)? Commented May 14, 2015 at 3:40
  • 1
    @leekaiinthesky It wouldn't. If it is Python3 (which seems to be the case) then input returns a string and int('3.3') would return ValueError. (If OP isn't using Py3, I strongly advise to switch to raw_input instead of input) The behavior you describe happens when applying int to a float though, but not its literal. Commented May 14, 2015 at 3:47

2 Answers 2

2

Why do you have a nested loop? (Two while True one inside the other one)

The following code is simpler and should work:

def rolldice():
    while True:
        num_sides = input("Enter number of sides for die: ")
        num_rolls = input("Enter number of rolls for die: ")
        try:
            int(num_sides)
            int(num_rolls)
            break
        except ValueError:
            print("Enter an integer")

If both int evaluate and do not crash then break is executed and you can leave the loop.

As it is a function, you might want to return (num_sides, num_rolls) at the end of the function so they aren't lost. (You could also replace the break with the return, which will effectively stop the function too at the same time)

Unless this is only the beginning of your code, then never mind what I just said. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Right, there's more to the code...but it wasn't relevant to my question. Thanks for the help
0

You can just cast int and check for a ValueError:

def rolldice():
    while True:
        try:
            num_sides = int(input("Enter number of sides for die: "))
            num_rolls = int(input("Enter number of rolls for die: "))
            break
        except ValueError:
            print("Enter an integer")

>>> rolldice()
Enter number of sides for die: 5
Enter number of rolls for die: foo
Enter an integer
Enter number of sides for die: foo 
Enter an integer
Enter number of sides for die: bar
Enter an integer
Enter number of sides for die: 1
Enter number of rolls for die: 2
>>> 

4 Comments

Use isinstance() to check/confirm the type of an object.
Shorter than my version. Behaves a bit different than my version, I sticked with OP's format but this is perfectly good too!
@wwii Not very Pythonic I would say (is_instance in general, mainly because of "duck typing").
@JeromeJ ... I thought I was commenting on an answer that was using type in a Boolean expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.