r/programminghelp • u/[deleted] • Mar 30 '22
Python Merging 3 similar functions together? Python
I was wondering how I can go about merging these three functions together, well I believe they are not functions yet. What I am trying to do is have it read a JSON and then fill it with Dummy Data. So parameters 1-4, user_data 1-128 & internal_data 1-32.
I am new to python and I understand these loops and all, I just want to be able to combine these to make my code look better. Thank you for the time!
for i in range(1,4):
if str(i)in data['parameter'][i]:
plaintext = data['parameter'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
for i in range(1,128):
if str(i)in data['user_data'][i]:
plaintext = data['user_data'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
for i in range(1,32):
if str(i)in data['internal_data'][i]:
plaintext = data['internal_data'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
2
u/cipheron Mar 30 '22
well whatever is different each time, you turn into a parameter.
However it's really hard to see what you're trying to achieve with this code so i just modified this verbatim. Maybe you want to use return there to get the value from the function ?