Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 NEWBIE HELP: How to tell Python when to use a function
So I know how to pull up an image using python, however I was hoping to make a program that would call a predetermined image based on a random number...
I.e. I say x=random.randrange(5)
if x=1 it calls a pic of a big #1, 2 calls #2 and so on and so forth.
So what I have right now is a program that calls a picture of #1, and it works (lets say this fully functional program is called funOne(jpg))

here's what I'm trying to do

if x =1:
     #commandThatImLookingForHere funOne(jpg)
if x =2:
     #commandThatImLookingForHere funTwo(jpgTwo)

etc etc.

I'm sorry if that was hard to understand, I've been up for a good number of hours. And I thank you for any help you guys can provide.
Hello and welcome to the forum!
If I understand right what you are trying to do, you seem to be on the right way. You don't need any special command in place of " #commandThatImLookingForHere". If you already have your functions (funOne(jpg), funTwo(jpgTwo)) written/defined, you can call them at that point with, say, "funOne(jpg)".

Some extra tips... you might want to look into "elif" statements, to replace your series of ifs, but your code will still work as it is now. And "else" is usually a good thing to add in the end, to deal with situations not covered with previous ifs/elifs. Also, your functions funOne(jpg), funTwo(jpgTwo), don't need to be 2 (or more..) functions. You can make 1 function that takes as argument which jpg to be shown, and then only place proper arguments (depending on if statements). Good luck coding!
A simple demo that is close to what you want.
import webbrowser
import random
import os

def rand_numb():
   numb = random.randrange(1, 6)
   return numb

def pics(numb):
   for img in os.listdir():
       if numb in img:

if __name__ == '__main__':
   numb = str(rand_numb())
So in folder 5 images(pic_1.jpg, pic_2.jpg..ect).
No path is given so you have the script in same folder as images.
Will random pick 1 image,and show it it browser or OS image viewer.
I do apologize gentlemen, but it appears as I somewhat overthought this one.
You see, I had already tried to do the command using only the function... so when you told me there was no special command I got pretty frustrated.
I had set it up in a very basic way, just to test if it would work. I tried to set up
if 3>2:

but I didn't set it up like that. I ACTUALLY set it up as
if 3<2:

I appreciate the help, and I think I learned a pretty valuable lesson today... Never overlook the little things
Oh I see, it happens =)
In future for quick testing result of a statement/operation/function, you can use Python's console. Type in e.g.:
>>> 3>2
And you'll get "true". Anyway, I suppose you figured that method already...

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)