infinite loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdmytryk
    New Member
    • Sep 2007
    • 12

    infinite loop

    i have an assignment where i need a bunch of random numbers up to 1000 so i figured id try to write a quick program. Ive been working on projects all day and an new to programming so bear with me if this is trivial. why is this an infinite loop?

    Code:
    import random
    
    times = raw_input("how many numbers to generate: ")
    count = 0
    
    while count <= times:
        number = random.randrange(999)+1
        print number
        count = count+1
    Last edited by bartonc; Oct 3 '07, 12:50 AM. Reason: Added [CODE][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by mdmytryk
    i have an assignment where i need a bunch of random numbers up to 1000 so i figured id try to write a quick program. Ive been working on projects all day and an new to programming so bear with me if this is trivial. why is this an infinite loop?

    Code:
    import random
    
    times = raw_input("how many numbers to generate: ")
    count = 0
    
    while count <= times:
        number = random.randrange(999)+1
        print number
        count = count+1
    That's because your times variable is a string. Here's a pretty good integer input pattern:[CODE=python]
    while True:
    try:
    times = int(raw_input(" how many numbers to generate: "))
    break # break out of the loop when you get a valid convertion
    except ValueError:
    pass[/CODE]Please note the use of [CODE] tags per instructions on the right hand side of the page while you are posting. Thanks.

    Comment

    • mdmytryk
      New Member
      • Sep 2007
      • 12

      #3
      sorry about the tags, thanks for your help. btw...how the hell are you guys so fast at all hours of the day???

      Comment

      Working...