Author Topic: Python Help - If Statements  (Read 840 times)

I'm trying to do a program with a gui where it picks out a random name i've entered in (Not through asking and storing a string) but through entering it with an if statement like so -
Code: [Select]
import random

cnum = random.randint(1, 115)
if cnum == 1:
cnum = "Aatrox"
elif cnum == 2:
cnum = "Ahri"

and then it says through a gui what it cnum returns as (Like Ahri or Aatrox) i'm doing this 115 times but I want to know if there are more efficent methods rather than 115 ifs/elifs/elses.
Python pro's halp

Note: I have the gui and stuff all done I just want to know if theres a more efficent way of storing names then picking one out of random.

Use an array.

Code: [Select]
import random

names = ['blah', 'more blah', 'thing', 'foobar', 'examples', 'forget your stuff']
cnum = names[random.randint(len(names))]

So with an array can you explain the last line?
like does len(names) random how many names there are -

like there are 50 names and len(names) is just meaning len(50)?
it's giving me an error, i'm using latest python by the way and coding it in IDLE
« Last Edit: July 22, 2013, 05:21:28 AM by Starzy »

Ah, this should actually be
Code: [Select]
cnum = names[random.randint(0, len(names) - 1)]
Because of 0-indexing. That, and I forgot to give the first parameter to randint, haha.

And yeah, len(names) literally translates to "the length of the names array", so if it had 50 names in it, len(names) would be 50.

So with an array can you explain the last line?
like does len(names) random how many names there are -

like there are 50 names and len(names) is just meaning len(50)?
it's giving me an error

Len(names) would return 50 if there was a list of 50 names.
random.randint(x) chooses a number between zero and x.

An alternative to Kingdaro's method would be to use random.choice()

Code: [Select]
import random

names = ["name", "foo", "name#2"]
cnum = random.choice(names)

Thanks a lot, also can I print/call how many names there are?
Like print (names) ??
« Last Edit: July 22, 2013, 05:36:32 AM by Starzy »

Just "print len(names)" I believe.

So i'm trying to put it in a gui using a label and the pack method and having trouble with len names

Code: [Select]
label2 = Label(start, text = len(names))
label2.pack(side = "right")
and because of the names being defined in
Code: [Select]
def random():
I can't put the label inside of it because it'll duplicate like 500 times and I can't put it out of it because it isn't define, is there anyway to define it outside and inside of
Code: [Select]
def random():
« Last Edit: July 22, 2013, 06:00:06 AM by Starzy »

Yeah, Python's scope is weird. You would need to define it once outside of the random() function, then use the "global" keyword to edit it from inside the random() function.

names = []

def random():
  global names
  # do whatever you want with names

yeah i'm using global right now :I

Thanks a lot for your help, I appreciate it a lot.

gonna leave this topic open for more discussion about python.
« Last Edit: July 22, 2013, 06:01:23 AM by Starzy »

Yeah, Python's scope is weird.

Weird as in totally normal? Global is such a stuff suggestion, you either define the names in the function or you pass them into the function.

Weird as in totally normal? Global is such a stuff suggestion, you either define the names in the function or you pass them into the function.
I thought global was the only way to do it, I'm new to this coding with python only like 4 months.



When I do the names = [] then go into the random(): function then set the names = [""] stuff, I then use Label2 = Label(start, text = len(names)) It always returns 0 anyway to fix this, I'm an idiot because I haven't used Len much

- I'm an idiot I didn't global names. Can't check if that's the problem cause I'm in bed and on iPod.
Didn't work still returns 0.
« Last Edit: July 22, 2013, 07:17:48 PM by Starzy »

new to this coding with python only like 4 months.
what