Python Forum
NEWBIE HELP: How to tell Python when to use a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NEWBIE HELP: How to tell Python when to use a function (/thread-1247.html)



NEWBIE HELP: How to tell Python when to use a function - BoaVenom18 - Dec-17-2016

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

x=random.randrange(5)
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.


RE: NEWBIE HELP: How to tell Python when to use a function - j.crater - Dec-17-2016

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!


RE: NEWBIE HELP: How to tell Python when to use a function - snippsat - Dec-17-2016

A simple demo that is close to what you want.
#rand_pick.py
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:
           webbrowser.open(img)

if __name__ == '__main__':
   numb = str(rand_numb())
   pics(numb)
So in folder 5 images(pic_1.jpg, pic_2.jpg..ect).
No path is given so you have the script rand_pick.py in same folder as images.
Will random pick 1 image,and show it it browser or OS image viewer.


RE: NEWBIE HELP: How to tell Python when to use a function - BoaVenom18 - Dec-17-2016

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:
coolOne(jpg)

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

I appreciate the help, and I think I learned a pretty valuable lesson today... Never overlook the little things


RE: NEWBIE HELP: How to tell Python when to use a function - j.crater - Dec-17-2016

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...