This is my code:
while True:
chosenUser = raw_input("Enter a username: ")
with open("userDetails.txt", "r") as userDetailsFile:
for line in userDetailsFile:
if chosenUser in line:
print "\n"
print chosenUser, "has taken previous tests. "
break
else:
print "That username is not registered."
Even after entering a username and it outputting results, the loop continues and asks me to input the username again.
I have recently asked a similar question but got it working myself. This one is not working no matter what I try.
Any ideas how to fix it?
Note: userDetailsFile is a text file that is in the program earlier.
The problem might be obvious but I'm quite new to Python so I'm sorry if I'm wasting anyone's time.
3 Answers 3
As others have noted, the primary problem is that the break only breaks from the inner for loop, not from the outer while loop, which can be fixed using e.g. a boolean variable or return.
But unless you want the "not registered" line to be printed for every line that does not contain the user's name, you should use for/else instead of if/else. However, instead of using a for loop, you could also just use next with a generator expression to get the right line (if any).
while True:
chosenUser = raw_input("Enter a username: ")
with open("userDetails.txt", "r") as userDetailsFile:
lineWithUser = next((line for line in userDetailsFile if chosenUser in line), None)
if lineWithUser is not None:
print "\n"
print chosenUser, "has taken previous tests. "
break # will break from while now
else:
print "That username is not registered."
Or if you do not actually need the lineWithUser, just use any:
while True:
chosenUser = raw_input("Enter a username: ")
with open("userDetails.txt", "r") as userDetailsFile:
if any(chosenUser in line for line in userDetailsFile):
print "\n"
print chosenUser, "has taken previous tests. "
break # will break from while now
else:
print "That username is not registered."
This way, the code is also much more compact and easier to read/understand what it's doing.
2 Comments
lineWithUser = next((line in userDetailsFile if chosenUser in line), None) The syntax error is the line) brcaket.next generator; however, on closer inspection you don't even need it, any is enough, making it much simpler.The easiest way to work around this is to make the outer while alterable by using a boolean variable, and toggling it:
loop = True
while loop:
for x in y:
if exit_condition:
loop = False
break
That way you can stop the outer loop from within an inner one.
Comments
The break breaks out the the inner for loop only.
Better put everything in a function and stop your while loop with a return:
def main():
while True:
chosenUser = raw_input("Enter a username: ")
with open("userDetails.txt", "r") as userDetailsFile:
for line in userDetailsFile:
if chosenUser in line:
print "\n"
print chosenUser, "has taken previous tests. "
return # stops the while loop
print "That username is not registered."
main()
print 'keep going here'
6 Comments
else should actually be after the for (to execute if the for finished normally) but that won't work with return.main(). You can also use different name than main if you prefer.
breakapplies to theforloop and terminates it early. It does not affect thewhile Trueloop.