As part of a large python program I have the following code:
for arg in sys.argv:
if "name=" in arg:
name = arg[5:]
print(name)
elif "uname=" in arg:
uname = arg[6:]
print(uname)
elif "password=" in arg:
password = arg[9:]
print(password)
elif "bday=" in arg:
bday = arg[5:]
print(bday)
else:
pass
The program expects something like:
python prog.py "name=Kevin" "uname=kevin" "password=something" "bday=01/01/01"
When I try to use “uname” later, the program fails, claiming “uname is not defined”
I added the “print()” lines to try and debug and the “print(uname)” always shows “=kevin” regardless of the index number I put there (here “6:”). The other statements seem to work fine. Is this a bug in python? I am very confused.
Thanks in advance.
The elif “uname=” is never run because the string “name=” is in “uname=”. Essentially, you are overwriting your name variable.
You could reorder your ifs so that so that the uname occurs before the name one.