2013-04-19 2 views
1

나는이 기본적인 Magic 8 Ball 프로그램을 사용하고 있는데, 예 또는 아니오로만 대답하도록 만들려고 노력하고 있는데, 첫 번째 단어 "will"이나 " do "내가 어떻게 그 단어를 허용하는 것을 만들 수 있습니까?기본 Python 스크립트 If Statment

import random 
import time 
print "Welcome to Magic Eight Ball !" 
while True: 
    def Magic8(a): 
     foo = ['Yes', 'No', 'Maybe', 'Doubtful', 'Try Again'] 
     from random import choice 
     print choice(foo) 


    a = raw_input("Question: ") 
    if a == "exit": 
     exit() 
    #If Stament here 
    print "Determining Your Future..." 
    time.sleep(2) 
    Magic8(a) 

답변

3
if a.split(None, 1)[0].lower() in {'will', 'do'}: 
    print "Determining Your Future..." 
    time.sleep(2) 
    magic8(a) # function names are usually not capitalized 

str.split()str.lower는 대문자를 처리해야 공백에 문장을 분할 : 여기

는 스크립트입니다.

+0

가이 일을 주셔서 감사합니다처럼 당신이 ELIF하거나 또는

if a=="exit": exit() elif a=="optionOne": doSomething else: print "word not alowed" 

을 사용할 수 있습니다 또는 당신이 할 수있는 작은 단어가 아마도 경우! – Serial

2

str.startswith을 사용하고 수락 된 단어의 튜플을 전달할 수 있습니다.

if a.lower().startswith(("will ", "do ")): 
    # Do your magic. 
+0

내 대답보다 좋고 간단하지만 어쩌면 거기에 공백을 추가해야 할 것입니다 : 그렇지 않으면 거짓 반응이있을 수 있습니다 : "윌리엄은 유니콘"입니다. 또한 자본화를 설명해야합니다. –

+0

@LevLevitsky ahh true! 고마워. – msvalkon

1

당신이

if a=="exit" or a=="notAllowedWord": 
    exit()