基本Python脚本If语句
我在胡闹这个基本的魔法8球程序,我试图让它只允许是或否的问题,我想它只会除了第一个单词“将”或“做”的问题,我怎么能做一个只允许这些单词的答案 以下是脚本:基本Python脚本If语句,python,if-statement,Python,If Statement,我在胡闹这个基本的魔法8球程序,我试图让它只允许是或否的问题,我想它只会除了第一个单词“将”或“做”的问题,我怎么能做一个只允许这些单词的答案 以下是脚本: import random import time print "Welcome to Magic Eight Ball !" while True: def Magic8(a): foo = ['Yes', 'No', 'Maybe', 'Doubtful', 'Try Again'] from r
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)
在空白处拆分句子,应处理大写字母
在空白处拆分句子,应处理大写。您可以使用
str.startswith
并传递接受单词的元组
if a.lower().startswith(("will ", "do ")):
# Do your magic.
您可以使用
str.startswith
并传递接受单词的元组
if a.lower().startswith(("will ", "do ")):
# Do your magic.
如果你有小词,你可以用elif或or
if a=="exit":
exit()
elif a=="optionOne":
doSomething
else:
print "word not alowed"
或者你可以这样做
if a=="exit" or a=="notAllowedWord":
exit()
如果你有小词,你可以用elif或or
if a=="exit":
exit()
elif a=="optionOne":
doSomething
else:
print "word not alowed"
或者你可以这样做
if a=="exit" or a=="notAllowedWord":
exit()
很好,比我的答案简单,但你可能必须在那里添加一个空格:
'will'
,'do'
,否则可能会出现误报:'William is unicorn'
。你还需要考虑大写。这很好,比我的答案更简单,但你可能需要在那里添加一个空格:'will'
,'do'
,否则可能会出现误报:'William is unicorn'
。你还需要考虑资本化。