为什么不将其追加到列表中?

时间:2018-11-18 10:04:46

标签: python

所以我正在制作这个程序,您可以在其中向其他用户发送笔记,但是当我尝试这样做时:

它不起作用。 我编写并发送了便笺,但是当我以其他用户身份登录(仍然执行相同的操作)时,没有收到便笺。

import datetime

#initialise stuff
class Account:
    def __init__(self, un, pw, notes, sent, received):
        self.un = un
        self.pw = pw
        self.notes = notes
        self.received = received
        self.sent = sent

class SentNote:
    def __init__(self, time, note, sender, recipient):
        self.time = time
        self.note = note
        self.sender = sender
        self.recipient = recipient

usernm = ""
passwd = ""
accounts = [Account("Eleeza", "Password", [], [], []), Account("User", "Password", [], [], [])]

signedinas = Account("", "", [], [], [])
#account signing up
def signmenu():
    while True:    
        option = input("Sign [i]n or sign [u]p? >>> ").lower()

        if option == "u":
            signup()
        if option == "i":
            signin()

def signup():
    usernm = input("Make a username >>> ")
    passwd = input("Make a password >>> ")
    accounts.append(Account(usernm, passwd, [], [], []))

def signin():
    inun = input("Username? >>> ")
    inpw = input("Password? >>> ")
    for account in accounts:
        if account.un == inun:
            if account.pw == inpw:
                print("\nSigned in!")
                signedinas.un = account.un
                signedinas.pw = account.pw
                signedinas.notes = account.notes
                appusage()
            else:
                print("Password is incorrect")

def appusage():
    print("Welcome, " + signedinas.un + "!")
    #ask what to do:
    while True:
        print("\nMain Menu\n")
        print("[S]end notes")
        print("[R]eceived notes ({0})".format(len(signedinas.received)))
        print("Sign [O]ut")
        whattodo = input("What would you like to do? >>> ").lower()
            #send note
        if whattodo == "s":
            print("\nSend a note")
            to = input("Username of who you're sending it to? >>> ")
            send = SentNote(datetime.datetime.now(), "", to, signedinas.un)
            print("Write your note:")
            send.note = input("")
            signedinas.sent.append(send)
            for user in accounts:
                if user.un == to:
                    user.received.append(send)
            print("Sent note!")
        if whattodo == "r":
            print("View Received Notes")
            for n in signedinas.received:
                print("From " + n.sender + " at " + str(n.time))
                print(n.note)
                viewoption = input("[N]ext note [B]ack to main menu >>> ").lower()
                if viewoption == "n":
                    continue
                if viewoption == "b":
                    break
        #sign out
        if whattodo == "o":
            print("See you soon!")
            break
signmenu()

2 个答案:

答案 0 :(得分:0)

signedinas是一个完全独立的Account对象;因此,它不会与accounts列表中的对象共享信息。

而不是这些行

signedinas.un = account.un
signedinas.pw = account.pw
signedinas.notes = account.notes

您应该只拥有signedinas = account,然后signedinas.received可能会更好。

第二,您将登录len(accounts)次到同一帐户,因为您注销后不会清除输入的输入,因此循环将重复以检查前一个account.un == inun , 例如。要解决此问题,应在appusage()调用之后使用单行代码

for account in accounts:
    if account.un == inun and account.pw == inpw:
        print("\nSigned in!")
        signedinas = account
        appusage()
        inun = inpw = None  # Add this

使用参数比使用全局signedinas变量要好。例如

def appusage(account):
    print("Welcome, " + account.un + "!")
    print("You have {} received messages.".format(len(account.received))

答案 1 :(得分:0)

accounts = [Account("Eleeza", "Password", [], [], []), Account("User", "Password", [], [], [])]

在此,Python创建了account对象。 在此:

user.received.append(send)

Python将消息从帐户列表保存到用户实例,但仅在当前上下文中。帐户列表相等:

[Account("Eleeza", "Password", [], [], []), Account("User", "Password", [], [], [])]

每次程序关闭并再次运行时。 您需要将帐户数据存储在filesdatabase中。

Saving an Object (Data persistence)