Python Forum
NEWBIE HELP: How to tell Python when to use a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NEWBIE HELP: How to tell Python when to use a function
#1
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.
Reply
#2
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!
Reply
#3
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.
Reply
#4
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
Reply
#5
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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newbie laleebee 2 1,286 May-24-2022, 01:39 PM
Last Post: laleebee
  Newbie on Python syntax rud0lp20 6 2,877 Apr-21-2020, 04:26 PM
Last Post: jefsummers
  python newbie marcush929 2 2,561 Jan-01-2020, 08:06 AM
Last Post: marcush929
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,579 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Python Linting (newbie) LieveHeer 2 2,579 Jan-24-2019, 05:36 PM
Last Post: LieveHeer
  Python newbie is asking Marcus_Gondwe 4 3,377 Apr-04-2018, 11:08 AM
Last Post: Marcus_Gondwe
  Newbie ? Python 3 Input() jlgrunr 1 2,429 Feb-17-2018, 10:26 PM
Last Post: Gribouillis
  Python Newbie phylon 1 26,933 Jan-09-2018, 10:46 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020