我陷入了无限循环

时间:2018-06-07 08:58:08

标签: python database python-3.x

我正在编写一个数据库,在这样做时我陷入了无限的困境  循环。

#!/bin/usr/python3  import os, sys import math

try:
    project_file = open('work_good.txt', 'w+')
    text = project_file.read() 

except FileNotFoundError as error:
    text = ('File not found')
    print (text)
    project_file.close()

with open('work_good.txt', 'r') as f:
    my_names = []    
    my_names = f.readlines()

def Zone():
    #Introduction to the database
    print ('WELCOME TO THE DATABASE', file=project_file)
    print (23*'=', file=project_file)


def Username():
    #Ask for users username
    usr_name = input ('What do you want your current username to be?: ').strip().capitalize()
    print ('Your username is: {}'.format(usr_name), file=project_file)
    return usr_name
         def Change_Username():
    #Information for the user to change their username
    change_usr = input ('Would you like to change your username? (y/n): ').strip().lower()

    if change_usr == 'y':
        status = input ('Enter your new username: ').strip()
        print ('Username is changing...', file=project_file)
        print ('Your new username is: {}'.format(status), file=project_file)

    elif change_usr == 'n':
        pass

    else:
        print ('No such command was given!', file=project_file)
     def Password():
    set_password = input ('What will be your password?: ').strip()
    password = ('Your password is: {}'.format(set_password))
    print (password, file=project_file)

def Password_Change():
    change_password = input('Do you want to change your password? (y/n): ').strip().lower()

    if change_password == 'y':
        password1 = input ('What will your new passsword be: ')
        password2 = ('Your new password is: {}'.format(password1))
        print (password2, file=project_file)

    elif change_password == 'n':
        print ('Your password remains the same'.upper(), file=project_file)

    else:
        print ('No such command has been given!', file=project_file)

Zone()

counter = 0

while counter <= 3:
    usr_name = []
    usr_name = Username()
    my_names.append(usr_name)
    print (usr_name, file=project_file)

Change_Username() Password() Password_Change()

我认为在while循环的最后一行是问题所在 可能会撒谎。

我尝试过添加if和else语句但是失败了。

我也试过添加一个break,pass和一个continue,但是有  失败了。

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

您的计数器为0,您永远不会再次更新它,因此它永远比3小一点。你应该在每次运行后增加它,在while循环中添加另一行:

counter += 1 # This increment the variable by 1 on each loop

当它到达4时,你的循环将结束