Python Forum
Feedback on my first program?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Feedback on my first program?
#5
One afterthought as well Smile

You can go one step further. From lists I suggested previously to dictionary. Lists help get rid of long statements on if/elif rows. Using dictionary you can get rid of elif statements altogether. Following technique is useful when you want to run function based on allowed user inputs.

Instead of list you create dictionary like that:

execute = {
    **dict.fromkeys(['play a game', 'game', 'play'], list_games), 
    **dict.fromkeys(['joke', 'hear a joke', 'hear', 'tell jokes'], tell_jokes),
    **dict.fromkeys(['have my mind blown', 'mind blown', 'have your mind blown'], blow_mind)
    }
Now you have dictionary with key: value pairs where key is allowed user input and value is name of the function you want to run after receiving input. By having this data structure you can take advantage of How do I use strings to call functions/methods?

In function wutNow you can now do this way:

if chosen_task in execute.keys():
    execute[chosen_task]()
This will run function corresponding to user input, no need for additional elif statements.

NB - I used PEP-8 compliant variable/function names which differ from names in your code. You should make sure that function and variable names match if you want to try this.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Feedback on my first program? - by jibby - Jun-12-2018, 04:20 AM
RE: Feedback on my first program? - by perfringo - Jun-12-2018, 06:35 AM
RE: Feedback on my first program? - by jibby - Jun-12-2018, 10:40 AM
RE: Feedback on my first program? - by killerrex - Jun-12-2018, 08:47 AM
RE: Feedback on my first program? - by perfringo - Jun-14-2018, 06:38 AM
RE: Feedback on my first program? - by jibby - Jun-18-2018, 04:09 AM
RE: Feedback on my first program? - by WolfWayfarer - Jul-16-2018, 08:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  First program feedback xyzabc12310000 2 3,511 May-20-2018, 05:06 PM
Last Post: xyzabc12310000
  I need some feedback on this program tannishpage 3 3,286 Mar-22-2018, 05:31 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

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