Is my code correct? experimenting with try, except, finally

d = {“a”: 1, “b”: 2, “c”: 3, “d”: 4, “e”: 5}
def Guess(x,y):
if x in d.keys() and y in d.values():
return True
else:
return False

print(“Guess the letter and number”)
try:

y = int(input("enter number: "))
x = str(input("enter letter: " ))

Guess(x,y)
print(Guess(x,y))

except ValueError:
print(‘Value Error: Invalid inputs!’)
except Exception as e:
print(e)

finally:
print(‘Program Finished’)

Whether it is correct depends on what you wanted it to do.

Superficially, this will work. There’s are some things worth
criticising.

d = {“a”: 1, “b”: 2, “c”: 3, “d”: 4, “e”: 5}

You can also write this:

d = dict(a=1, b=2, c=3, d=4, e=5)

Which you prefer is up to you.

def Guess(x,y):

Usually we give function names all lowercase names. Names with a single
leading uppercase letter are conventionally used for classes. Entirely
to aid people having an idea of what they’re looking at when they read
your code. Legal but unconventional.

if x in d.keys() and y in d.values():
return True
else:
return False

Any time you write:

if some-condition:
    return True
else:
    return False

you can always just write:

return some-condition

because the return values are just what you expect to get from the
condition anyway. A plain return s easier to read, as otherwise the
reader needs to look at the condition and then at each return to see
what (arbitrary) value it may be returning. Without the if-statement the
reader knows the return is the condition.

print(“Guess the letter and number”)
try:
y = int(input("enter number: "))
x = str(input("enter letter: " ))

The return from input() is already a str, no need to convert it for “x”.

Guess(x,y)
print(Guess(x,y))

except ValueError:
print(‘Value Error: Invalid inputs!’)

I would want to know the exception:

except ValueError as e:
    print('Value Error: Invalid inputs!', e)

except Exception as e:
print(e)

While this prints the exception, it gives you no traceback information.
Better to drop this clause altogether and let these get out. A “bare”
except (and similarly a very very broad excpet, such as the “except
Exception” above) is almost always a bad idea.

finally:
print(‘Program Finished’)

Sure.

generally it is best to put as little as possible inside a try/except,
because if you catch an exception (to handle it) you have less idea
where it came from the more code inside the try/except (could have
happened anywhere). A more normal setup would look like:

try:
    y = int(input("enter number: "))
except ValueError as e:
    print("bad number", e)
else:
    x = input("enter letter: " )
    print(Guess(x, y))

The rule of thumb is to only catch exceptions you can handle
meaningfully. For example if you opened a file to read it but got a
FileNotFound exception, you might catch that and behave as if the
missing file were empty. But Other exceptions might mean a more serious
problem: if you don’t know the correct behaviour for that situation,
don’t try. Thus the dropping of the “except Exception” in the example
above.

Cheers,
Cameron Simpson cs@cskk.id.au

This will look ok if someone entered ‘a’ and 5 while I think you’re after ‘a’ and 1 or ‘e’ and 5 etc.
In which case replace the above code with:

if (x,y) in d.items():

This will check that an x key has a y value.