1

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.

asked Jan 30, 2018 at 19:59
1
  • 1
    break applies to the for loop and terminates it early. It does not affect the while True loop. Commented Jan 30, 2018 at 20:00

3 Answers 3

2

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.

answered Jan 30, 2018 at 20:13
Sign up to request clarification or add additional context in comments.

2 Comments

That seems quite complex and I'm unfamiliar with this. I'm getting a syntax error in: lineWithUser = next((line in userDetailsFile if chosenUser in line), None) The syntax error is the line) brcaket.
@Programmer12 There was a mistake in the next generator; however, on closer inspection you don't even need it, any is enough, making it much simpler.
0

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.

answered Jan 30, 2018 at 20:04

Comments

0

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'
answered Jan 30, 2018 at 20:05

6 Comments

That seems to completely end my whole program though even if I have functions after it?
This will not work properly, if else should actually be after the for (to execute if the for finished normally) but that won't work with return.
@Programmer12 Why should it exit your program? You can write code after main(). You can also use different name than main if you prefer.
No, sorry. It's still completely ending my program. (I'm using Python 2.7 if that has anything to do with it?)
I have this after my code: ` print "\n", "You can now exit the quiz or choose another report." while True: userDecision = raw_input("Choose 'report' or 'exit'").lower() if userDecision == 'report': chooseReport() break elif userDecision == "quit": sys.exit()`
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.