简体   繁体   中英

Nestled if else and loop help python 3

Two questions:

  1. How can I nestle if + else statements between other if + else statements?
  2. How can I loop back to the top of my code when the else statement is activated?

Here's the code:

print("Welcome To Your Workstation.")
usrname = input("Username: ")
pssword = input("Password: ")
if usrname=="Harry" and pssword=="password123":
    print("-=-=-=Welcome=-=-=-")
    print("Enter A Program Name")
    print("You can use:add_up")
    program = input(":")
    if program=="add_up":
            print("Ok, Lets Get Adding!")
            num1 = input(int(float("Enter Your First Number: ")
            num2 = input(int(float("Enter Your Second Number: ")
            num3 = input(int(float("Enter Your Third Number: ")
            answer = num1 + num2 + num3
            print(num1"+"num2"+"num3"="answer)
    else:
        print("Unknown Command, Reboot And Try Again.")

else:
    print("Incorrect Password, Reboot And Try Again"

I am a python noob in need of help :)

You want to use a while loop. It works like this:

while expr:
    stuff to do

expr can be any valid expression, just like you would use in an if statement.

stuff to do can be any code, including more loops, if statements and so on.

There are a couple of ways to exit a while loop:

  1. Make expr false. For example, if your expression is len(myword) < 10 , add some more characters to myword . The value of expr is only evaluated between each run of your stuff to do block.
  2. Raise an exception. This is usually used when problems arise, and will make your code jump all the way up the stack to a matching except statement, catching the exception.
  3. Use the break keyword, which immediately stops the nearest enclosing loop.

In your case, alternatives 1 or 3 are the best candidates. They would look something like this:

keeprunning = True
while keeprunning:
    if something:
        keeprunning = False
    print('this will still be printed after keeprunning was changed')

and

while True:
    if something:
        break
    print('this won\'t be printed if the break is executed')

Yes, use a while loop.

print("Welcome To Your Workstation.")
usrname = input("Username: ")
pssword = input("Password: ")
while True:
    if usrname=="Harry" and pssword=="password123":
        print("-=-=-=Welcome=-=-=-")
        print("Enter A Program Name")
        print("You can use:add_up")
        program = input(":")
        if program=="add_up":
            print("Ok, Lets Get Adding!")
            num1 = input(int(float("Enter Your First Number: ")
            num2 = input(int(float("Enter Your Second Number: ")
            num3 = input(int(float("Enter Your Third Number: ")
            answer = num1 + num2 + num3
            print(num1"+"num2"+"num3"="answer)
        break
   else:
        print("Unknown Command, Reboot And Try Again.")

else:
    print("Incorrect Password, Reboot And Try Again"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM