国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)的簡(jiǎn)單算術(shù)游戲?qū)嵗?/h1>
2020-02-23 01:26:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)的簡(jiǎn)單算術(shù)游戲。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

#!/usr/bin/env pythonfrom operator import add, sub from random import randint, choiceops = {'+': add, '-':sub}#定義一個(gè)字典MAXTRIES = 2 def doprob():  op = choice('+-')  #用choice從'+-'中隨意選擇操作符   nums = [randint(1,10) for i in range(2)]  #用randint(1,10)隨機(jī)生成一個(gè)1到10的數(shù),隨機(jī)兩次使用range(2)   nums.sort(reverse=True)  #按升序排序  ans = ops[op](*nums)  #利用函數(shù)  pr = '%d %s %d = ' % (nums[0], op, nums[1])  oops = 0   #oops用來(lái)計(jì)算failure測(cè)試,當(dāng)三次時(shí)自動(dòng)給出答案  while True:    try:      if int(raw_input(pr)) == ans:        print 'correct'        break      if oops == MAXTRIES:        print 'answer/n %s%d' % (pr, ans)        break      else:        print 'incorrect... try again'        oops += 1    except (KeyboardInterrupt, EOFError, ValueError):      print 'invalid ipnut... try again'def main():  while True:    doprob()    try:      opt = raw_input('Again? [y]').lower()      if opt and opt[0] == 'n':        break    except (KeyboardInterrupt, EOFError):      breakif __name__ == '__main__':  main()

運(yùn)行結(jié)果如下:

8 - 1 = 7correctAgain? [y]y7 - 1 = 6correctAgain? [y]y9 + 4 = 0incorrect... try again9 + 4 = 

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 孟村| 耿马| 饶平县| 威远县| 清涧县| 类乌齐县| 明水县| 祁连县| 云浮市| 专栏| 孙吴县| 大田县| 福建省| 大兴区| 含山县| 广州市| 施甸县| 蒙山县| 兴隆县| 曲水县| 黎平县| 呈贡县| 泗水县| 景泰县| 宣恩县| 长治县| 江门市| 堆龙德庆县| 凯里市| 沧州市| 区。| 郁南县| 太康县| 佛冈县| 天峨县| 阿拉尔市| 峡江县| 于田县| 乌审旗| 兰考县| 晋中市|