Ataulf
Ataulf

Reputation: 33

If comparison statements in Python

This block of code returns "cat", "dog", "hamster", and "unicorn", but it shouldn't return "unicorn" at all! Is there any reason for this?

if random.randint(0,10) < 5:
    print("dog")    
elif  random.randint(0,10) > 5:
    print("cat")    
elif  random.randint(0,10) == 5:
    print("hamster")    
else:
    print("unicorn")

Upvotes: 3

Views: 457

Answers (8)

xxmbabanexx
xxmbabanexx

Reputation: 8696

You need to create only one random integer.

Your code should be:

myRandom = random.randint(0,10)

if myRandom < 5:
    print("dog")    
elif  myRandom > 5:
    print("cat")    
elif  myRandom == 5:
    print("hamster")    
else:
    print("unicorn") 

Upvotes: 0

HerrKaputt
HerrKaputt

Reputation: 2604

You're generating three different random numbers. What you're thinking is this:

random_number = random.randint(0,10)

if random_number < 5:
    print("dog")

elif random_number > 5:
    print("cat")

elif random_number == 5:
    print("hamster")

else:
    print("unicorn")

This code will only return one word, and will never return "unicorn".

Upvotes: 0

alestanis
alestanis

Reputation: 21863

Your random number is different everytime you call random.randint so it might be 7 when you test the first if and go past it, then 3, then 4, and bam, you're in unicorn.

You should call random.randint only once at the beginning of your if, save its value and check it instead.

myrand = random.randint(0,10)
if myrand < 5:
  print("dog")    
elif myrand > 5:
  print("cat")    
elif myrand == 5:
  print("hamster")    
else:
  print("unicorn")

Upvotes: 1

vartec
vartec

Reputation: 134581

You're getting new random number on each comparison. What you probably meant is:

my_random_int = random.randint(0,10)

if my_random_int < 5:
    print("dog")

elif  my_random_int > 5:
    print("cat")

elif my_random_int == 5:
    print("hamster")

else:
    print("unicorn")

Upvotes: 8

Pavel Anossov
Pavel Anossov

Reputation: 62878

Assuming correct indentation, there's no reason for three random ints to be respectively >=5, <=5, and "not 5".

You probably meant to do this:

value = random.randint(0, 10)

if value < 5:
    print("dog")
elif value > 5:
    print("cat")
elif value == 5:
    print("hamster")
else:
    print("unicorn")

Now there are no chances of unicorns.

Upvotes: 2

David Zwicker
David Zwicker

Reputation: 24268

You should create the random number only once!

val = random.randint(0,10)
if val < 5:
    print("dog")

elif val > 5:
    print("cat")

elif val == 5:
    print("hamster")

else:
    print("unicorn")

Upvotes: 3

jonvuri
jonvuri

Reputation: 5920

The issue here is that you're generating a new random number each time. You should create it once and then assign it to a variable, then check that.

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61488

random.randint is called again each time it is reached, potentially producing a different result each time (since that is the function's purpose).

If you want to repeatedly test with the same value, then store the value first.

Upvotes: 4

Related Questions