Little Foot
Little Foot

Reputation: 97

Function using While True not working as expected. Asks for input Twice

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)   

Upvotes: 0

Views: 84

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

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) 

Upvotes: 2

Related Questions