标签:编写 python use oar cancel 加减法 加法 int span
#!/usr/bin/env python import random import sys def add(x,y): return x + y def sub(x,y): return x - y def probe(): CMDs = {‘+‘:add,‘-‘:sub} alist = [random.randint(1,50) for i in range(2)] alist.sort(reverse=True) op = random.choice(CMDs.keys()) answer = CMDs[op](*alist) prompt = """%s %s %s:""" %(alist[0],op,alist[1]) i = 0 while i < 3: try: result = int(raw_input(prompt)) except (KeyboardInterrupt,EOFError),e: print "\nUser cancelled" sys.exit() except (ValueError,IndexError,UnboundLocalError): print "Invaild" if answer == result: print "very good" break print "input wrong still %d chance" % (2-i) i += 1 else: print ("%s%s") % (prompt,answer) if __name__ == ‘__main__‘: while True: probe() yn = raw_input("continue(y/n)?>:".strip()[0]) if yn in ‘Nn‘: print "bye-bye" break
标签:编写 python use oar cancel 加减法 加法 int span
原文地址:http://www.cnblogs.com/weiwenbo/p/6646340.html