Spookichicken Spookichicken - 2 months ago 7
Python Question

How to Detect an Empty Value

I'm currently making a Python script for account creation. My script is as follows:

import csv
nms = []
with open('nms.txt', newline='') as inputfile:
for row in csv.reader(inputfile):
nms.append(row)
counter = 0
## End of tings
while True:
print ("Please enter your user name. This must be unique.")
usernm = input ("")
while True:
if nms[1][counter] != (""):
print (".")
else:
break
if usernm == nms[1][counter]:
print ("Unfortunately this user name is in use. Please try a different user name.")


counter = counter + 1

print ("end")


The issue is that with the way I've made my script, where I've tried to stop the program checking if the username is equal to a blank value, it of course errors, is there a better method of detecting a null value? So that when it actually see's one, it will break out of the while loop? The rest of my program thus far seems to work fine.

Answer

I'm not sure I see the need for the counter variable, but you can check if an item exists in an iterable object with in

if usernm in nms:
    # user already exists 

I've tried to stop the program checking if the username is equal to a blank value

It looks like you checked if the second value of nms at the character of the index counter is an empty string. You didn't check the username that was input at all.

if usernm.trim() == "":
    # username is blank 
Comments