| Author |
Topic  |
|
|
djas559
Newbie
USA
4 Posts |
Posted - 10/26/2009 : 10:48:30 PM
|
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
|
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 |
 |
|
|
djas559
Newbie
USA
4 Posts |
Posted - 10/26/2009 : 11:46:54 PM
|
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. |
 |
|
|
sakuramboo
Moderator
    
USA
9836 Posts |
Posted - 10/26/2009 : 11:51:43 PM
|
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 |
 |
|
|
family guy
Advanced Member
    
www.c0deb0x.net
2747 Posts |
Posted - 10/28/2009 : 7:10:07 PM
|
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 |
 |
|
|
djas559
Newbie
USA
4 Posts |
Posted - 10/30/2009 : 03:56:30 AM
|
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? |
 |
|
|
sakuramboo
Moderator
    
USA
9836 Posts |
Posted - 10/30/2009 : 09:06:23 AM
|
| 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 |
 |
|
|
family guy
Advanced Member
    
www.c0deb0x.net
2747 Posts |
Posted - 10/30/2009 : 4:46:49 PM
|
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 |
 |
|
| |
Topic  |
|