2017-08-01 67 views
-1

如果我有一個列表說uinput = str(raw_input("Enter you input : a|b|c|d|quit")) 我想當我選擇只有一個然後執行後它應該要求「按任意鍵繼續」,如果我選擇一個| b | c然後當c執行然後它應該要求「按任意鍵繼續」並退出選項它應該給這個提示。停止終端在列表執行

請幫助我,我們該如何做到這一點。感謝您的幫助

謝謝

+0

'uinput'是一個字符串,而不是一個列表。 – DeepSpace

+0

@ DeepSpace是正確的抱歉。如果我這樣做uinput1 = uinput.split(「|」) – Ritesh

回答

0

這是你想要的嗎?

def foo(): 
    usr_input = raw_input("Enter you input : a|b|c|d|quit\n") 
    usr_input_list = usr_input.split('|') 

    length_of_list = len(usr_input_list) 

    for usr_input_element in usr_input_list: 
     if usr_input_element == 'a': 
      if length_of_list == 1: # Only if there's only an 'a' in list 
       press_any_key_to_continue() 
     if usr_input_element == 'c': 
      if length_of_list > 1: # If there is other things than 'c' in list 
       press_any_key_to_continue() 
     if usr_input_element == 'quit': 
      exit() 

def press_any_key_to_continue(): 
    this_variable_will_not_be_used = raw_input("Press any key to continue\n") 
    print("Now it will continue") 

如果你輸入 「A | C | B | A |退出| d」 它會創建一個列表,看起來像這樣

[ '一', 'C',「B 」,‘A’,‘退出’,‘d’]

和列表的長度則是:6

然後for循環將通過列表,一個接一個。


或者你可以這樣做:

for usr_input_element in usr_input_list: 
    if length_of_list == 1: # Only if there's only an 'a' in list 
     if usr_input_element == 'a': 
      press_any_key_to_continue() 
     if usr_input_element == 'c': 
      press_any_key_to_continue() 

    if length_of_list > 1: # If there is other things than 'c' in list 
     if usr_input_element == 'a': 
      press_any_key_to_continue() 
     if usr_input_element == 'c': 
      press_any_key_to_continue() 

    if usr_input_element == 'quit': 
     exit() 

但是請注意,然後你把所有的,如果把兩者if length_of_list == 1:和​​內他們的陳述內容。

+0

OuuGii謝謝。是的,我知道這一點。但是把這個放在我的上面senerio – Ritesh

+0

@Ritesh我更新了我的答案,好不好? – OuuGiii

+0

@Ritesh你是否想讓usr輸入東西,直到他輸入'quit'? – OuuGiii