python loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python loop

okay, about the while loop at the very end, at the "else" statement.. at the end i forgot to add a "continue" but it still loops back up.... why?? """ from cryptography.fernet import Fernet def write_key(): key = Fernet.generate_key() with open('key.key', 'wb') as key_file: key_file.write(key) def load_key(): file = open('key.key', 'rb') key = file.read() file.close() return key key = load_key() fer = Fernet(key) def view(): with open('passwords.txt', 'r') as f: for line in f.readlines(): data = (line.rstrip()) user, passw = data.split('|') print('user:', user + '| Password:', fer.decrypt(passw.encode()).decode()) def add(): while True: name = input('Account Name: ').lower() if name.isdigit(): print('no numbers') continue else: break password = input('Password: ') with open('passwords.txt', 'a') as f: f.write(name + '|' + fer.encrypt(password.encode()).decode() + '\n') while True: mod = input('view the existing passwords or add new ones or quit? (view/add/quit): ').lower() if mod == 'q' or mod == 'quit': print('bye!') quit() if mod == 'view' or mod == 'v': print('') view() print('') elif mod == 'add' or mod == 'a': add() else: print('not a valid option')

7th Aug 2021, 9:36 PM
Tomoe
Tomoe - avatar
4 Answers
+ 1
If you don't get a false statement or break it always loops back why should it stop and continue just stops lines after it from running
7th Aug 2021, 9:55 PM
Abs Sh
Abs Sh - avatar
+ 1
You basically have an infinite loop, because there is no end condition in the while test (only True, which means an infinite loop sans inner break statements), and there are no break statements in the body of the "while" loop. I suggest putting a break statement somewhere in there, probably after the "quit()" function, since quit is being tested for in that if block.
7th Aug 2021, 10:25 PM
BootInk
BootInk - avatar
0
BootInk I don't think quit() needs break any more put it in the else
7th Aug 2021, 10:36 PM
Abs Sh
Abs Sh - avatar
0
@Abs Sh, I see what you mean. However, putting it in the else block wouldn't make much sense. Why give the user only one chance to answer if they accidentally put in a wrong prompt, like "ad", or "qui". It just breaks off without a retry? Not a good user experience in my opinion. So from what I see, it makes sense to have that functionality. I actually forgot what quit was for, it's been so long since I've used it. My bad on that one.
7th Aug 2021, 11:42 PM
BootInk
BootInk - avatar