1

I have a problem with if and else statements.

I wrote the following script:

def questionscp():
    stateanswer = raw_input('Do you want to change device state? (y/n): ')
    if stateanswer == 'y':
        choosestate = raw_input('Choose Device State(0=Hibernate, 1=Power Save, 2=Safe Mode, 3=Operating Mode, 4=Diagnostic Mode, 5=Remote Power Off): ')
        client.write_register(64, choosestate, 70)
    else:
        return
    activewarningflag = raw_input('Activate warning flag?? (y/n): ')
    if activewarningflag == 'y':
        client.write_register(67, 1, 70)
    else:
        return

The problem is that I want the script will ask the second question (activewarningflag) after the first one, even if 'n' is chosen, but in the else statement I put "return" cause Im new to do this and dont know a lot yet. What can I write instead of "return" if I want the function to continue to read itself?

thanks

2
  • 1
    You don't need to have an else statement at all, its also possible you don't even need a return Commented Jan 21, 2015 at 13:42
  • you might want to use a while loop and verify input Commented Jan 21, 2015 at 13:53

2 Answers 2

5

You can put pass as in

else:
        pass

This will make the interpreter ignore your else clause.

Or

you can ignore the else altogether, as Python semantics allows an if statement without an else clause

Sign up to request clarification or add additional context in comments.

Comments

2

You don't necessarily need the else statement at all

if stateanswer == 'y':
    choosestate = raw_input('...')
    client.write_register(64, choosestate, 70)
activewarningflag = raw_input('Activate warning flag?? (y/n): ')

You can leave it out and it will continue as normal.

You also may not even need the return (depending on what your method does). Returning is normally either done to return a value from the method or to exit the method early.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.