#🔒 Error in lopp with " Registration " simulation in PY

78 messages · Page 1 of 1 (latest)

lime dockBOT
#

@dusk spoke

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dusk spoke
#

I have no idea why it deleted OG message.

#

Basicly.

Code is ment to mimic a registration process.

  1. Create Name. (Works)
  2. Create Password. (Works)
    2.5 Check if password is X long and has Number included (Works)
  3. Repeat Password. (Works)
    3.1 If Password wrong Try again (3 Trys)
  4. If password wrong 3 times ask.
    Do you want to reset your password or not with ( yes / no ) input (works)
    4.1 If yes -> Create new password (works partialy it ends codes imidietly after)
    4.2 If no -> end programm (works)

this work will be graded.
I asked the teacher and he couldnt help me find how to resolve it.

#

Error in lopp with " Registration " simulation in PY

shadow olive
#

i see way too many nested while loops, we could simplify that which would in the end resolve the issue here 🙂

dusk spoke
#

I had a 2 day crash course on Python

#

All I know is
If -> elif -> else -> def -> _ _ intit _ _

shadow olive
#
max_versuche = 3
while max_versuche > 0: # password ok, break the loop and finish
    if benutzer.passwort_bestätigen(passwort_eingabe):
        break
    else if max_versuche > 0: # wrong password, decrement try count and go again
        max_versuche -= 1 
    else: # 3 wrong tries, reset mechanism
        while True:
            entscheidung = input("Möchten Sie Ihr Passwort neu festlegen? (ja/nein): ")
            if entscheidung not in ["ja", "nein"]: # this ensures the input will be valid here
                continue 
            if entscheidung == "ja":
                generate_password(benutzer)
                break
            else:
                print("Versuchen Sie es erneut.")
                break
        else:
            break
else:
    print("no more tries")
#

i would change this part a bit

def passwort_festlegen(self, passwort: str) -> bool:                                 # Akzeptiert ein Passwort als Parameter 
        if len(passwort) < 5 or not re.search(r'\d', passwort):
            print("Das Passwort muss mindestens 5 Zeichen lang sein und mindestens eine Zahl enthalten.")
            return False
        else:
            self.passwort = passwort                                        # "passwort" weist auf Atribut (self.passwort)
            print("Passwort erfolgreich festgelegt.")
            return True
#

then generate_password would be a helper function

def generate_password(benutzer: Benutzer) -> None:
    while True:
        neues_passwort = input("Geben Sie Ihr neues Passwort ein: ")
        if benutzer.passwort_festlegen(neues_passwort):
            break
#

please try it, and let me know if you have any trouble understanding so i can explain 🙂 @dusk spoke

dusk spoke
#

My prof said something a continue function a second ago... Where you can freely go around?
Let me give your code+ a try first

shadow olive
#

continue keyword works inside of for or while loop, what it does is immediately stopping code execution at its line and starts next iteration in loop.. somewhat similar to break except it doesn't end the whole loop, just the current iteration

dusk spoke
#

could be the indets are off?

#

SyntaxError: 'break' outside loop

shadow olive
#

let me check

#

so when you create a user and provide password, then fail password entry 3 times you are asked to reset the password - after successful reset Passwort erfolgreich festgelegt. is printed, but you are not asked to login again, should you be? 🙂

#

after user input "nein" to password reset promt, program prints Versuchen Sie es erneut and program ends

dusk spoke
#

It works

#

r/Ihadastroke

shadow olive
#

xd

#

saw that

#

ok then, so after password reset, program should not promt user to log in again right?

#

then it works like intented 🙂

dusk spoke
#

it should i fixed that by

#

adding the anmelde_versche = 0

shadow olive
#

in that case, i'd just move login in a function and call it again

#

after reset, do you only need password again or also username ?

dusk spoke
#

Im doing just password

shadow olive
#

like - repeat whole login process, or only allow login for the user that reset the password

#

hmmm, ok, we can fix that

#

it would be a tiny bit more advanced tho

dusk spoke
#

I made a programm work order thingy

shadow olive
#

using recursion

dusk spoke
#

I am not sure what its called in english

#

This was the basic Idea

#

Translation:

  1. Input " Name "
    1.1 (Set?) " Name " as
    1.2 Ask for Password Input
  2. Input Password
    2.1 Set Password as
    2.2. Ask por Password confirmation
  3. Input Password
  4. IF Password1=Password then End else go back to step 1.2
#

Hope it makse sense

shadow olive
#

yes it makes perfect sense, great that you drew the program before coding!
are you familiar with recursion?

#

here's what i would do -> first login attempt would be for "New login"
after login fails and user resets the password successfully, login process if restarted, but this time we provide user that attempts to log in (line 63) login(benutzer)

#

this way, the first part is skipped, as we already have the user object

    if not benutzer:
        name = input("Geben Sie Ihren Namen ein: ")
        benutzer = Benutzer(name)

        # Passwort festlegen
        while True:
            passwort = input("Legen Sie Ihr Passwort fest: ")
            benutzer.passwort_festlegen(passwort)
            if benutzer.passwort:
                break
#

i believe it will make sense to you, question is if it's usable as recursive design is a tiny bit more advanced, would your teacher approve of this?

#

if not, you can still use your loop, just wanted to share this "cleaner" way with you

dusk spoke
#

Approve yes for sure.
No im not familiar with recursion

shadow olive
#

it's basically calling the function from itself

dusk spoke
#

I probably pasted it wrong but this happend

#

Name
Password
Wrong Passwort
Confirm password

#

And we are back to square 1 issue whise

shadow olive
#

!e

def rec(n: int):
    if n > 5:
        return
    print(f"recursion {n}")
    n += 1
    rec(n)

rec(0)
lime dockBOT
#

@shadow olive :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | recursion 0
002 | recursion 1
003 | recursion 2
004 | recursion 3
005 | recursion 4
006 | recursion 5
shadow olive
dusk spoke
shadow olive
#
if __name__ == "__main__":
    login()
#

that's all you need

#

all the logic will be included inside login fucntion

#

just Control + a code from the link and paste 🙂 it should work

#

you only call the login() function once, program only ends in 2 ways:

  1. user logs in successfully
  2. user fails to log in and rejects to reset password
dusk spoke
#

Now it asks be 3 times to confirm my password

#

I just copy pasted the code

#

Ill defintly have to do some homework to properly understand this tho

shadow olive
# dusk spoke

i see, forgot to set the flag in this case on line 49

if benutzer.passwort_bestätigen(passwort_eingabe):
    reset = False
    break
dusk spoke
#

Forget it

#

wait

#

I pasted it in my original code

#

It does till repeat 3 times tho

shadow olive
#

oh, im sorry o didnt realize that you actually have to use return keyword in recursion instead of just breaking the loop, because the function from which recursion was called is still running

#

now, this is the final and working version

#

difference is, break only breaks the loop but the rest of the code is still running, return stops the function execution and returns a value if required

#

i have to go, if you have more questions later just let me know

dusk spoke
#

It works like a charm

#

Ty for the help

#

I'll make sure to check out the topic of recursion after this

lime dockBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.