0

I can't seem to get my while loop code to run inside my code. I bet it is very obvious but I cannot seem to find the answer for it. This program is supposed to let you choose how many numbers you want to have randomly chosen and the numbers it can be between. It seems that the while loop doesn't want to work. It skips the while loop and goes to the sleep(10). Thank you for the help!

import random
import time
from time import sleep
x = raw_input("Enter first number you want to be the minimum: ")
y = raw_input("Enter second number you want to be the maximum: ")
a = raw_input("Enter ammount of random numbers you want: ")
p = 1
while p >= a:
 print "Your number is " + str(int(random.randint(x - 1,y + 1)))
 p = p + 1
sleep(10)
asked May 15, 2014 at 20:29
3
  • 1
    I think you meant while p <= a: Commented May 15, 2014 at 20:32
  • should not it have been while p<=a. since you are incrementing p inside the while loop Commented May 15, 2014 at 20:33
  • Had you used for _ in range(a):, which doesn't require the counter, this problem would have been easier to spot. Commented May 15, 2014 at 20:38

1 Answer 1

4

raw_input returns a string. This means you are comparing a string to an integer for your while condition. A quick test shows integers are always "less than" strings.

>>> 10000 > '1'
False
>>> 10000 < '1'
True

Luckily, this behavior is changed in python3 where it throws a TypeError.

answered May 15, 2014 at 20:33
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I changed raw_input("Enter first number you want to be the minimum: ") to int(raw_input("Enter first number you want to be the minimum: "))
No problem - glad I could help. Would you mind accepting my answer if it helped you out?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.