Reputation: 1245
I am learning how to use do not repat yourself principle (https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) in Python.
See my block of code below:
for key, value in my_dictionary.items():
if "," in value:
if key != value:
raise ValueError(f"This is my error !")
else:
if key != value + ",00":
raise ValueError(f"This is my error !")
You can see that I repeat the part raise ValueError(f"This is my error !")
two times. Is it posible to use it only once? I tried to use for/else method, but I got lost. Thank you very much for help.
Upvotes: 2
Views: 122
Reputation: 43169
You could use
for key, value in my_dictionary.items():
if ("," in value and key != value) or (key != value + ",00"):
raise ValueError(f"This is my error !")
# everything seems to be fine
# do sth. useful here
Upvotes: 3