#🔒 is there an optimized way to complete this task?

80 messages · Page 1 of 1 (latest)

boreal saddle
#

I was messing around with if-else statements and "while True" as well. I decided to make a simple login page with it. I wanted to know if there was an easier way to do it. I was wondering if there was a way to combine them or something to make it less lengthy. I needed several 'while True' statements or it just loops back to the beginning when ran.

https://paste.pythondiscord.com/GVMQ

ocean edgeBOT
#

@boreal saddle

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.

boreal saddle
#

The code works, I was just wondering if there was an easier way to do it

swift obsidian
#

first few lines here i found something

#
user = input("Enter Username: ")
``` the input also prints to the terminal. ill keep reading
#

your 3 while trues are also concerning. instead of opening a new while true within the original while, break it with

break
``` which forces the while loop to end and make your new loop from there.

it reduces indentation making code easier to read
#

@boreal saddle

#

you also had an extra while True which i didnt catch until just now but i edited my message to fix that

boreal saddle
#

alright thanks

swift obsidian
#

do you see what i did?

#

bc something telling me you are about to just copy this without asking any questions as to why which is why ive deleted it now

boreal saddle
#

yea, I tried to use the break but i did it wrong i think

#

1 sec

#

after the break, do i need another while True?

swift obsidian
#

sorry if you werent going to do that, but a lot of people who say alright thanks just immediately copy the message and close the chat without seeing why its done that way

#

but yes you would put another while true after the break with no indention

boreal saddle
#

nah, i wasn't. I'm taking a course as well. I'm actually trying to learn

#

ill post what I have after I update it

swift obsidian
#

while you edit can you send the task itself? some of these homework questions have weird catches to them

#

*im assuming its an assignment from the wording of the title

boreal saddle
#

nah, i was just learning about if, elif and else and thought i would make something myself

swift obsidian
#

alright, now that gives us more freedom

boreal saddle
#

after the break, the script ends? I added the while True

swift obsidian
#

can you send what you have? prob a syntax error

boreal saddle
#

this is the first part of it, it stops after the break

swift obsidian
#

your indention seems off, can you please upload the whole thing? it could just be a paste error

boreal saddle
#

ok

swift obsidian
#

so the break leaves the closest loop entirely. move your second loop all the way to the left. as is your program physically cannot reach the second loop

#

i also just finished the better way to do this once we get this working too to show you how it should be done

#

but only after you manage to get it working so you can learn the fundamentals

boreal saddle
#

alright, i think i see what you're saying now. I'll update the three loops and post what i have. Thanks for the help by the way.

#

The confusing part for me was the while True statement indentations. I thought because it was in a new code block it needed to be indented past the break line

swift obsidian
#

nope, the break exits the loop entirely so to start a new loop you have to make it outside the scope of the broken loop

#

always start new code blocks as far left as possible

#

more indentions = bad

boreal saddle
#

ah ok

swift obsidian
#

this remake ive done for you has at most 3 indentions and that 3rd indention is only returns and prints

#

i should prob clarify - im not using multiple while true loops like you are so if youre stuck trying to figure out how to use fewer indentions dont worry about it. as long as you understand

boreal saddle
#

ah ok. I think i got it. uploading it to pastebin rn

#

like this?

swift obsidian
#

looks right, big question now. does it work?

boreal saddle
#

it does

swift obsidian
#

alright, one moment then. im having my local ai generate comments for you for this code

boreal saddle
#

i tried it with the right login and the wrong login and it worked both ways

#

with the wrong login it looped as I wanted it to

swift obsidian
#

https://paste.pythondiscord.com/Z4DA

now heres how i would do it - minus the plain text credentials but thats beyond what youre doing today

please ask me questions but hopefully the sheer amount of comments should help you (thank you my friend cody the ai)

#

and before you ask, no i dont use the ai to code(most of the time) i mostly use it to generate comments and documentation bc i work in a group and i get yelled at for not properly documenting XD

boreal saddle
#

yea i know adding comments can be a bit tedious

swift obsidian
#

so any questions on what i sent? i know its wildly different than what you made but it does the same thing using better practices (minus credentials being plain text lol)

boreal saddle
#

i think i get everything except for how the authentication part works.

swift obsidian
#

alright, just ask which part or do you want me to just break down the whole line?

boreal saddle
#

wait nvm, authentication and authenticate are different variables

swift obsidian
#

do you understand how i called the command you enter? im betting thats the farthest out there concept in there

boreal saddle
#

yea i don't get that part

#

what is the f"{user}/home: do?

swift obsidian
#

so thats an f string. instead of ""+"" you can do f"{variable} basic text" to insert variable data without a bunch of +'s. it also does some other cool things but thats the basic function. lets you insert text

boreal saddle
#

oh

swift obsidian
#

the /home was just to indicate it wasnt the login portion

boreal saddle
#

oh i see

swift obsidian
#

with it the user being user i got confused myself and thought it didnt work until i realized what happened so i added the /home

boreal saddle
#

yea, i just used user as a placeholder

swift obsidian
#

i think my help channel is about to timeout lol. guess im asking again tomorrow

boreal saddle
#

I'm actually taking a SQL server college course so hopefully, I would be able to learn storing data in databases and such and integrate Python into what I'm learning about SQL. (to not have passwords in plain text)

swift obsidian
#

perfect, youll have it figured out in no time then

boreal saddle
#

thanks for your help, and I hope your channel doesn't get timed out

swift obsidian
#

it prob will my problem is kinda out there

boreal saddle
#

i would help if i knew more lol

swift obsidian
#

unless you know ast XD

boreal saddle
#

nah I'm kinda just starting if you couldn't tell

#

This is like day one for me

swift obsidian
#

oh my bad i thought you were a master programmer here to teach me

boreal saddle
#

lol

swift obsidian
#

yup theres the timeout. guess im asking tomorrow, enjoy learning... while you can

boreal saddle
#

yea im trying. Thanks for your help

ocean edgeBOT
#
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.