0

I saw some posts on this but it didn't really answer my question This is my code:

if os.path.exists(KEYSAVE_PATH):
    with open (KEYSAVE_PATH, "r") as file:
        data = file.readline()
    keyauthapp.login(data)

else:
    key = input('Key: ')
    keyauthapp.login(key)
    keysave = open(KEYSAVE_PATH, "w")
    n = keysave.write(key)
    keysave.close()
    /////MY CODE\\\\\

/////MY CODE\\\\\ is my code basically when I run this it runs smoothly but when I get to the end of the if it just stops and ends my program there aren't any errors it just thinks its the end of my project how do I make after the keyauthapp.login(data) it skips to my code bellow the else statement.

4
  • 1
    You know that indentation is important in python? If you want to leave the else bit, stop indenting. Feb 21, 2021 at 15:48
  • 1
    The entire purpose of a "else" statement is to run code only when the "if" statement is not true. So anything in the else statement is not going to run whenever code in the if statement runs. If you want the code to run in all cases, just don't put it in the if/else.
    – ICW
    Feb 21, 2021 at 15:50
  • A tangential note: In Python it is often recommended to use try-except rather than if-else (see for reference: devblogs.microsoft.com/python/idiomatic-python-eafp-versus-lbyl/…)
    – nehalem
    Feb 21, 2021 at 15:53
  • @Hex I attempted to make some edits to your post to improve readability by recommending some standard English protocols (Caps at beginning of sentence, periods at end of sentences). If this is helpful to you, here is another way to phrase your request : /////MY CODE\\\\\ is my code. Basically when I run this it runs smoothly but when I get to the end of the if it just stops and ends my program. There are no errors. It just thinks it is the end of my project. How do I make the code continue executing the code below the else after the keyauthapp.login(data) statement ? Feb 22, 2021 at 22:50

1 Answer 1

1

If both codes in the if-else does the same end goal which is do the login of your app then you can unindent your code to execute when it happens either way.

if os.path.exists(KEYSAVE_PATH):
    with open (KEYSAVE_PATH, "r") as file:
        data = file.readline()
    keyauthapp.login(data)

else:
    key = input('Key: ')
    keyauthapp.login(key)
    keysave = open(KEYSAVE_PATH, "w")
    n = keysave.write(key)
    keysave.close()

/////MY CODE WILL BE EXECUTED WHEN LOGIN HAPPENS\\\\\
4
  • ty is their a shortcut to unident my big chunk of code?
    – Hex
    Feb 21, 2021 at 16:08
  • Select your big chunk and do shift + tab Feb 21, 2021 at 16:13
  • You could press backspace four times. From there the automatic indent will be one block less. Otherwise try pressing Ctrl + [.
    – Jacob Lee
    Feb 21, 2021 at 16:18
  • Thanks Jacob TY TY TY
    – Hex
    Feb 21, 2021 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.