【问题标题】:Python - while loop ends when it shouldn'tPython - while循环在不应该结束时结束
【发布时间】:2016-06-14 17:41:42
【问题描述】:

这里是 Python 菜鸟。我正在编写一个简单的客户端/服务器程序,要求用户输入姓名、用户名、电子邮件地址、密码。该信息被发送到服务器,服务器检查该用户的文本文件中是否已经存在条目。如果有,它应该发回一条消息说这个用户已经存在,要求用户再试一次。

我将一个名为 flag 的变量设置为 False。我根据文本文件检查用户信息,如果在文件中找不到匹配项,我将 Flag 设置为 true。然后我有一个 if 语句,如果标志为 True,则在文件中写入用户信息。

但是,当我输入重复信息时,它会发回相应的“用户已存在”消息,但无论如何都会将重复信息写入 UserProfile.txt 文件中。我不断地重写我的循环和 if 语句以查看它是否会有所作为,但无论如何,我遇到了同样的问题。

from socket import *
from datetime import datetime
#Create a welcome socket bound at serverPort
serverPort = 12009
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(10)
print ('The Hello Name server is ready to receive')
accessTime = datetime.now();
print("Access time is", accessTime);
flag = False

while 1:

    while not flag:
        connectionSocket, addr = serverSocket.accept()
        #Wait for the hello message 
        sentence1 = connectionSocket.recv(1024)
        print("From", addr,sentence1.decode('ascii'))
        #Ask for name if a hello message is received
        if(sentence1.decode('ascii').upper() == "HELLO"):
            returnMessage1 = "Please provide the requested information."
        connectionSocket.send(returnMessage1.encode()) 
        #Wait for the name
        sentence2 = connectionSocket.recv(1024)
        fullName = sentence2.decode('ascii')
        #Wait for the email
        sentence3 = connectionSocket.recv(1024)
        email = sentence3.decode('ascii')
        #Wait for the username
        sentence4 = connectionSocket.recv(1024)
        userName = sentence4.decode('ascii')
        #Wait for the password
        sentence5 = connectionSocket.recv(1024)
        password = sentence5.decode('ascii')



        for line in open("UserProfile.txt").readlines():
            if line.find(userName) > -1:    #found the username in this record
                returnMessage3 = "Username already exists, please try again" #reject username
                connectionSocket.send(returnMessage3.encode())
            if line.find(fullName) > -1 and line.find(email) > -1:
                returnMessage4 = "Account already exists for this person, please try again" #reject email                 
            else:
                flag = True

    if flag:            

        #Prepare the access record with information separated by tab key
        userAccount = userName+"\t"+password+"\t"+fullName+"\t"+email+"\n"
        #Append the access record into accessRecord.txt
        output_file = open("UserProfile.txt", "a")
        output_file.write(userAccount)
        output_file.close()
        #Respond the client with the access information
        returnMessage2 = "Registration Successful"
        connectionSocket.send(returnMessage2.encode())

    connectionSocket.close()                #Close the connection

【问题讨论】:

  • 请仔细检查/修复您的缩进。您嵌套的 while 循环在语法上无效,这让我怀疑其余的缩进。
  • 现在应该修复了。

标签: python while-loop


【解决方案1】:

当您发现用户名已经存在时,您会发回回复,但您不会停止。

尝试在 if 块中添加 break

for line
    if user name in line
        send message about already exists
        break - out of for loop

【讨论】:

  • 解决了!我想我不得不承认我不完全确定为什么。如果循环没有中断,flag 不会仍然设置为 False 吗?为什么设置为 true 的 Else: 语句仍然执行第一个“if”语句?
  • 会,但是当 NEXT if 语句运行时会发生什么?未找到全名,ELSE 标志 = true!
  • 啊,有道理。谢谢!当它允许我时,我会接受。
猜你喜欢
  • 2014-04-20
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多