1

So I'm making this program where you can send notes to different users but when I try this:

It doesn't work. I write and send the note, but when I log in as the other user (still in the same execution) there is no received note.

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()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Please add a [mcve] of the inputs you are providing, and I suggest running this code via a debugger. – OneCricketeer Nov 18 '18 at 10:11
  • 1
    It seems like you're never calling `signin()` again after "logging out", so it's not clear how you are doing that. Or at least, `for account in accounts` is being iterated more than once, so you just see no received messages until you logout of all users – OneCricketeer Nov 18 '18 at 10:14

2 Answers2

0

signedinas is a completely separate Account object; therefore it does not share information with the objects within the accounts list.

Rather than these lines

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

You should just have signedinas = account, and then signedinas.received might work better.

Secondly, you are going to sign-in to the same account for len(accounts) times because you do not clear the entered inputs after logging out, and so the loop will repeat to check the previous account.un == inun, for example. To fix this, should be a one-liner after the appusage() call

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

Even better than a global signedinas variable, would be to use parameters. E.g.

def appusage(account):
    print("Welcome, " + account.un + "!")
    print("You have {} received messages.".format(len(account.received))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Hi there, the `signedinas = account` thing doesn't work for me, but from looking over that again I added `signedinas.received = account.received` and that one-liner you said to put after the `appusage()` call. The program now works :) Thanks – Eleeza the Other World Wizard Nov 19 '18 at 18:46
0
accounts = [Account("Eleeza", "Password", [], [], []), Account("User", "Password", [], [], [])]

in this, Python create the accounts objects. In this:

user.received.append(send)

Python save message to user instance from accounts list, but only in current context. Accounts list make equal:

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

every time when program close and run again. You need to store accounts data in files or database.

Saving an Object (Data persistence)

moveax3
  • 336
  • 1
  • 3
  • 10