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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.