0

The purpose of this function is to check the input of the user, determine if its in the list, and, if not, tells the user to ''Select one of the given options''.

The issue with it is that it asks me for input twice. When I select option ''Approach exit door'' it loops again and asks for input, when I enter the same option again thats when I get the result (which leads to dead() function not copied here to keep this short). Tried several things, like adding a condition that closes the loop and tries to make the While False so it stops, but it didnt work. Also, thanks to user khelwood who provided me with this alternative and simplified my program.

too long didnt read? Loop asks for input twice, cant fix it.

Heres the code.

s2option_list =['Approach exit door', 'Check for vital signs']

def sget_choice(s2option_list):
    while True:
        print s2option_list
        choice = raw_input('> ')
        for i, opt in enumerate(s2option_list):
            if opt in choice:
                 return i
            else:
                print "You need to select one of the given options."

def scenario2(response):
    print response
    print "Conversation"
    print "Conversation"
    print "Conversation"

    sget_choice(s2option_list)

    if sget_choice(s2option_list)==0:
        dead("Conversation")              
    else:
        print "Conversation"

        sget_choice2(s2option_list2)   
6
  • Also, you call sget_choice two times in scenario2. Once before and one inside of the if statement. Commented Dec 26, 2014 at 1:46
  • The sget_choice at the end is supposed to call another function with the same aim. So, Padraic, I am fairly new at this so I barely understand it. You mean that if I add a return 0 in scenario2 it will fix the problem? Commented Dec 26, 2014 at 1:49
  • @LittleFoot, I added an answer, I am not 100 percent on the == 0 though, do you only accept one option? Commented Dec 26, 2014 at 1:52
  • Well, as far as I know its supposed to point to the first option in the list. Else would be second option and then it will print/call another function. Commented Dec 26, 2014 at 1:53
  • ah ok scratch that, I missed the 2 I thought you called sget_choice again, your problem was you were calling the function twice, you need to just call once and store the return value and compare that or just use if sget_choice(s2option_list)==0 once and forget the previous call Commented Dec 26, 2014 at 1:53

1 Answer 1

2
def scenario2(response):
    print response
    print "Conversation"
    print "Conversation"
    print "Conversation"    
    res = sget_choice(s2option_list) # call it once save return value in a variable   
    if res == 0: # now you are checking the value from the previous call not calling again
        dead("Conversation")              
    else:
        print "Conversation"   
        sget_choice2(s2option_list2) 
Sign up to request clarification or add additional context in comments.

2 Comments

This fixed the function and pretty much everything. Thanks you people made my night.
No prob, you might consider giving the user a menu to pick from, might make life easier

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.