Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
import time import random #options= [ 0 1 2 3 4 5 6 7 8 9 #options = ["pine", "spruce", "fir", "cedar", "hemlock", "larch", "juniper", "yew", "sequoia", "cypress"] #Each Yes/No question leads to other questions for either Yes or No. #Throughout the questions, the function guess(x,y,z) gives the program a chance of guessing the answer. #Useful functions for simplicity def question(x): print x time.sleep(1) ans = raw_input(x).lower() print "You > '%s'" % ans print "" time.sleep(1) return ans def badResponse(): print "Invalid response!!! You must reply 'Yes' or 'No'!!!" time.sleep(1) print "Try again!!!" time.sleep(1) #continue def guess(x, y, z): gess = random.randint(x, y) chance = random.randint(x, y) tree = random.choice(z) if gess != chance: failure = "guess failed" #print failure return failure elif gess == chance: qn = raw_input("Is your tree %s ?" % tree) print "Is your tree %s ?" % tree if qn == "yes": time.sleep(1) print "You > 'yes'" time.sleep(2) print "YESSSS!!! I knew it was %s!!! Victory!!!" % tree return "yes" elif qn == "no": time.sleep(1) print "You > 'no'" time.sleep(1) print "Awww, I Lost! What was it?" time.sleep(1) correct = raw_input("What was the correct one-word answer?").lower() print correct time.sleep(1) print "I shall now study things about %s." % correct return "no" else: print "error" def react(x, y, z): time.sleep(1) rn = guess(x, y, z) return rn print "error" #Introductions print "I will try to guess what coniferous tree you are thinking of!" time.sleep(1) print "My options are: Pine, Spruce, Fir, Cedar, Hemlock, Larch, Juniper, Yew, Sequoia, and Cypress." time.sleep(1) print "I shall ask you questions; answer 'Yes' or 'No'!" print "" time.sleep(2) #Begin questions for n in range(1): #Q1 Yes Q1 = question("Does your tree have cones with scales?") if Q1 == "yes": options = ["pine", "spruce", "hemlock", "larch", "sequoia", "cypress"] ask = guess(1, 18, options) if ask == "guess failed": Q2A = question("Does your tree lose its leaves each year?") if Q2A == "yes": options = ["larch", "cypress"] ask = guess(1, 4, options) if ask == "guess failed": Q3A = question("Does your tree's cones break apart to release its seeds?") if Q3A == "yes": c1 = raw_input("Is your tree larch?").lower() print "Is your tree larch?" if c1 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was larch!" elif c1 == "no": print "You > 'no'" time.sleep(1) print "But I know it is larch! There aren't any more options!" else: badResponse() elif Q3A == "no": c2 = raw_input("Is your tree cypress?").lower() print "Is your tree cypress?" if c2 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was cypress!" elif c2 == "no": print "You > 'no'" time.sleep(1) print "But I know it is cypress! There aren't any more options!" else: badResponse() else: badResponse() elif ask == "yes" or "no": break else: print "error" elif Q2A == "no": options = ["pine", "spruce", "hemlock", "sequoia"] ask = guess(1, 8, options) if ask == "guess failed": Q3B = question("Does your tree bear needles?") if Q3B == "yes": options = ["pine", "spruce", "hemlock"] ask = guess(1, 5, options) if ask == "guess failed": Q4 = question("Does your tree usually bear needles in bundles of multiple needles?") if Q4 == "yes": d1 = raw_input("Is your tree pine?").lower() print "Is your tree pine?" if d1 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was pine!" elif d1 == "no": print "You > 'no'" time.sleep(1) print "But I know it is pine! There aren't any more options!" else: badResponse() elif Q4 == "no": options = ["hemlock", "spruce"] ask = guess(1, 2, options) if ask == "guess failed": Q5 = question("Does your tree's twigs have two rows of needles?") if Q5 == "yes": e1 = raw_input("Is your tree hemlock?").lower() print "Is your tree hemlock?" if e1 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was hemlock!" elif e1 == "no": print "You > 'no'" time.sleep(1) print "But I know it is hemlock! There aren't any more options!" else: badResponse() elif Q5 == "no": e2 = raw_input("Is your tree spruce?").lower() print "Is your tree spruce?" if e2 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was spruce!" elif e2 == "no": print "You > 'no'" time.sleep(1) print "But I know it is spruce! There aren't any more options!" else: badResponse() else: badResponse() elif ask == "yes" or "no": break else: print "error" else: badResponse() elif ask == "yes" or "no": break else: print "error" elif Q3B == "no": c3 = raw_input("Is your tree sequoia?").lower() print "Is your tree sequoia?" if c3 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was sequoia!" elif c3 == "no": print "You > 'no'" time.sleep(1) print "But I know it is sequoia! There aren't any more options!" else: badResponse() else: badResponse() elif ask == "yes" or "no": break else: print "error" else: badResponse() elif ask == "yes" or "no": break else: print "error" #Q1 No elif Q1 == "no": options = ["fir", "cedar", "juniper", "yew"] ask = guess(1, 12, options) if ask == "guess failed": Q2B = question("does your tree have fleshy or berrylike fruit?") if Q2B == "yes": options = ["juniper", "yew"] ask = guess(1, 3, options) if ask == "guess failed": Q4B = question("Does your tree have scale leaves and produces seeds in rounded, bluish fruits?") if Q4B == "yes": d2 = raw_input("Is your tree juniper?").lower() print "Is your tree juniper?" if d2 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was juniper!" elif d2 == "no": print "You > 'no'" time.sleep(1) print "But I know it is juniper! There aren't any more options!" else: badResponse() elif Q4B == "no": d3 = raw_input("Is your tree yew?").lower() print "Is your tree yew?" if d3 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was yew!" elif d3 == "no": print "You > 'no'" time.sleep(1) print "But I know it is yew! There aren't any more options!" else: badResponse() else: badResponse() elif ask == "yes" or "no": break else: print "error" #Q2.2 No elif Q2B == "no": options = ["fir", "cedar"] ask = guess(1, 3, options) if ask == "guess failed": Q3D = question("does your tree have stout, barrel-shaped cones?") if Q3D == "yes": c4 = raw_input("Is your tree cedar?").lower() print "Is your tree cedar?" if c4 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was cedar!" elif c4 == "no": print "You > 'no'" time.sleep(1) print "But I know it is cedar! There aren't any more options!" else: badResponse() elif Q3D == "no": c5 = raw_input("Is your tree fir?").lower() print "Is your tree fir?" if c5 == "yes": print "You > 'yes'" time.sleep(1) print "Yes!!! I knew it was fir!" elif c5 == "no": print "You > no'" time.sleep(1) print "But I know it is fir! There aren't any more options!" else: badResponse() else: badResponse() elif ask == "yes" or "no": break else: print "error" else: badResponse() elif ask == "yes" or "no": break else: print "error" else: badResponse()
ch_twentyQuestions.py
( around 290 lines python code )
Published By:
EM5700
Published on
2019-10-19T01:33:55Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here