سؤال

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)
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top