I am trying to write a simple program in python to read command line arguments and print a final word based on the arguments. If there is any argument of the form "-f=" then the will go to the front of the final printed word. Similarly for "-e=" the text goes to the back and if there is -caps as an argument then the final printed word will all be uppercase. I do a while loop to scan through the arguments and check for these flags. The full code is:
import sys
i=1
while i<len(sys.argv):frnt_wrd = Nonelst_wrd = None arg_1 = str(sys.argv[i])if arg_1.startswith('-f='):front = arg_1.split('=')frnt_wrd = front[1]elif arg_1.startswith('-e='):last = arg_1.split('=')lst_wrd = last[1]if arg_1.startswith('-caps'):caps = Trueelse:word = arg_1i+=1
print (front)
print (frnt_wrd)
I had a couple of if statements later on to print out the word based on whether frnt_wrd and lst_wrd were not equal to None (i.e. a value had been assigned to them) but nothing was printing out. To check the problem I did:
print (front)
print (frnt_wrd)
and the output actually gives me front as the desired array (when there is an argument of the form "-f=" but returns frnt_wrd as None even though I defined:
frnt_wrd = front[1]
When I type exactly this line outside of the while loop it actually works but why is it not defining the frnt_wrd inside the while loop?
Edit: The full code giving me frnt_wrd as None is above. What else do you need? I would like to learn how to do it with while and without argparse. I need to understand why I am defining a variable and it is not defining.
Traceback: enter image description here