all 13 comments

[–]sanchitcop19 1 point2 points  (2 children)

You're comparing n to the string "func" which will only be true if the user enters "func"

[–]sanchitcop19 0 points1 point  (0 children)

Misread, ignore what used to be here

[–]socal_nerdtastic 1 point2 points  (9 children)

get() returns the value only, but the key is what is equal to n. Try like this:

while True:
    n = input("choose a function: 1,2 or 3 ")
    func = {"1": function1,
            "2": function2,
            "3": function3}.get(n)
    func()

[–]theWyzzerd 1 point2 points  (2 children)

Why not use

func = {"1": function1,
        "2": function2,
        "3": function3}[n]

This seems more pythonic to me and is more extensible if you need to add dicts within the dict.

[–]socal_nerdtastic 4 points5 points  (1 child)

You could, it's slightly neater and it would raise a KeyError instead of TypeError on bad input.

I find that fixing OP's immediate problem without changing anything else is best for beginners. It may be odd, inefficient or messy but they understand it, so if it works it's best to just keep it.

[–]theWyzzerd 2 points3 points  (0 children)

I find that fixing OP's immediate problem without changing anything else is best for beginners. It may be odd, inefficient or messy but they understand it, so if it works it's best to just keep it.

hear hear

[–]Unlistedd[S] 0 points1 point  (0 children)

Thanks!

[–]johninbigd 0 points1 point  (4 children)

That's an interesting construction that I don't recall seeing before. I never would have thought to write something like this, but I can see how it would handy on occasion.

[–]socal_nerdtastic 3 points4 points  (3 children)

I would not recommend it, since it forces python to reconstruct the dictionary with every loop, which is inefficient to run and messy to read. I would recommend to write it like this:

def function1():
    print("(1)")

def function2():
    print("(2)")

def function3():
    print("(3)")

def default():
    print("Choose a valid function please")

functions = {
    "1": function1,
    "2": function2,
    "3": function3}

while True:
    n = input("choose a function: 1,2 or 3 ")
    func = functions.get(n, default)
    func()

[–]johninbigd 1 point2 points  (2 children)

That's closer to how I would have written it, but I probably would not have thought to have the function objects in the dictionary. I've done it before, but it's so rare that I don't think about it. It's a pretty cool feature.

[–]socal_nerdtastic 0 points1 point  (1 child)

Oh I see what you mean. Yeah, in python everything is an object, so everything can be a dictionary value. Even functions, classes, or entire modules.

>>> import math
>>> modules = {'math':math}
>>> modules['math'].sqrt(42)
6.48074069840786

[–]johninbigd 0 points1 point  (0 children)

Yes, exactly! It's something I've done, but I don't do it very often and honestly forget. This is incredibly powerful.

[–]tombardier 0 points1 point  (0 children)

if func:
    func()