Variable not defined in while loop in python?

2024/11/20 19:25:49

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

Answer

The problem is that you reset frnt_word and lst_word to None each time through the loop, so you're overwriting the value that was set by a previous argument. You should just initialize them before the loop.

frnt_word = None
lst_word = None
caps = False
word = None
for arg in sys.argv[1:]:optarr = arg.split("=")if optarr[0] == "-f":frnt_word = optarr[1]elif optarr[0] == "-e":lst_word = optarr[1]elif optarr[0] == "-caps":caps = Trueelse:word = arg
https://en.xdnf.cn/q/119791.html

Related Q&A

Hours and time converting to a certain format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Python socket server: listening to multiple clients [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

I have a problem with encoding with russian language for my python script [duplicate]

This question already has answers here:UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)(34 answers)Closed last year.I am trying to send an email fro…

how do you style data frame in Pandas

I have this data frame: dfServer Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc.…

Vacation price program Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Why did push of a Flask app to Heroku failed?

Im simply trying to push my Flask app to Heroku but I encountered the following error: remote: ERROR: Command errored out with exit status 1: remote: command: /app/.heroku/python…

How to navigate through HTMl pages that have paging for their content using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How to merge one list elements with another list elements in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Display and play audio files

I am new to python, and Im trying to build a simple recording program. With the some help from my previous question, I was able to add a timestamp for each recorded fileEDIT:I did some research and dec…

Web Scraping BeautifulSoup - Next Page parsing

Im just learning web scraping & want to output the result of this website to a csv file https://www.avbuyer.com/aircraft/private-jets but am struggling with parsing the next pages here is my code (…