J!NX Forums
J!NX Forums
Home | Forum Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
This username differs from J!NX login...
Save Password
Forgot your Password?

 All Forums
 TechSpeak
 Code Poets
 Python
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

djas559
Newbie

USA
4 Posts

Posted - 10/26/2009 :  10:48:30 PM  Show Profile  Reply with Quote
Hello Jinx, I have an infinite loop. How could I put a limit on how many random numbers are inserted into this list? say I only want 100 numbers to be created in the list. How could I go about that? I am fairly new to python. thanks for your time


import random

running = True
while running:
    a = random.randrange(1, 100000)
    b = []
    b.append(a)

sakuramboo
Moderator

USA
9836 Posts

Posted - 10/26/2009 :  11:12:07 PM  Show Profile  Send sakuramboo an AOL message  Send sakuramboo a Yahoo! Message  Reply with Quote
instead of,
while running:

use a counter.
while num != 100:
     <stuff>
     num++

"Anytime somebody puts a lock on something you own, against your wishes and doesn't give you a key, they are not doing it for your benefit." - Cory Doctorow
Go to Top of Page

djas559
Newbie

USA
4 Posts

Posted - 10/26/2009 :  11:46:54 PM  Show Profile  Reply with Quote
now I have this

import random
num = 0
while num != 100:
     a = random.randrange(1, 100000)
     b = []
     b.append(a)
     num++
print b


I have tried a few different things here


num++ 

Syntax Error

num +=1 

Infinite loop

num++1

infinite loop

There has to be something I am missing. Either the program crashes or its syntax is incorrect. Does python have an increment statment? I looked it up cant find straight answers.
Go to Top of Page

sakuramboo
Moderator

USA
9836 Posts

Posted - 10/26/2009 :  11:51:43 PM  Show Profile  Send sakuramboo an AOL message  Send sakuramboo a Yahoo! Message  Reply with Quote
Apparently Python doesn't support incrementers. They way to do it would be to do,

num = num + 1

instead of,

num++

"Anytime somebody puts a lock on something you own, against your wishes and doesn't give you a key, they are not doing it for your benefit." - Cory Doctorow
Go to Top of Page

family guy
Advanced Member

www.c0deb0x.net
2747 Posts

Posted - 10/28/2009 :  7:10:07 PM  Show Profile  Send family guy an AOL message  Click to see family guy's MSN Messenger address  Send family guy a Yahoo! Message  Reply with Quote
Python does support the var+= increment. But you've got another problem...


import random
num = 0
while num != 100:
     a = random.randrange(1, 100000)
     b = []
     b.append(a)
     num++
print b


In your code, you've placed the creation of your list 'b' inside of the while loop, this will create an empty list every iteration of the loop. Just move it above the while loop and use 'num+=1' increment.


import random
num = 0
b = []
while num != 100:
     a = random.randrange(1, 100000)
     b.append(a)
     num+=1
print b


------------------------------------------------------
Moving at 1/35 c with respect to what? Without defining a reference, it's like asking, "What the difference between a duck?" -Swansont
Studying philosophy 'scientifically' would be like stirring a spoon with some soup. -Sayonara
http://www.scienceforums.net

Edited by - family guy on 10/28/2009 7:12:03 PM
Go to Top of Page

djas559
Newbie

USA
4 Posts

Posted - 10/30/2009 :  03:56:30 AM  Show Profile  Reply with Quote
Ok thank you both for your time. i am fairly new to progamming

I also have a question regarding functions is there an easy way to pass variables and such between functions or make the functions communicate better? I am trying to write a small D&D based game for my older brother. nothing fancy just command line based fighting i have a rough draft but, I wanted to write a lot of modules for monsters and such. and I would need the characters stats to end up inside the modules somehow. any ideas?
Go to Top of Page

sakuramboo
Moderator

USA
9836 Posts

Posted - 10/30/2009 :  09:06:23 AM  Show Profile  Send sakuramboo an AOL message  Send sakuramboo a Yahoo! Message  Reply with Quote
Just like what family guy said, don't put the variables inside of the functions unless you want that variable to get created every time the function runs.

"Anytime somebody puts a lock on something you own, against your wishes and doesn't give you a key, they are not doing it for your benefit." - Cory Doctorow
Go to Top of Page

family guy
Advanced Member

www.c0deb0x.net
2747 Posts

Posted - 10/30/2009 :  4:46:49 PM  Show Profile  Send family guy an AOL message  Click to see family guy's MSN Messenger address  Send family guy a Yahoo! Message  Reply with Quote
You can pass variables between functions like so...


def function_name(variable1, variable 2)
    code using variable1
    code using variable2

variable1 = something
variable2 = something
function_name(variable1, variable2)


This will pass the variables to the function, you can return them and assign the returned values to something when calling the function.

------------------------------------------------------
Moving at 1/35 c with respect to what? Without defining a reference, it's like asking, "What the difference between a duck?" -Swansont
Studying philosophy 'scientifically' would be like stirring a spoon with some soup. -Sayonara
http://www.scienceforums.net
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
J!NX Forums © J!NX and Snitz Go To Top Of Page
Snitz Forums 2000