Python實現(xiàn)的簡單算術(shù)游戲?qū)嵗?/h1>
更新時間:2015年05月26日 10:16:01 作者:buaa_shang
這篇文章主要介紹了Python實現(xiàn)的簡單算術(shù)游戲,可實現(xiàn)隨機給出算數(shù)表達(dá)式,并對用戶輸入答案進(jìn)行判斷的功能,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)的簡單算術(shù)游戲。分享給大家供大家參考。具體實現(xiàn)方法如下:
#!/usr/bin/env python
from operator import add, sub
from random import randint, choice
ops = {'+': add, '-':sub}
#定義一個字典
MAXTRIES = 2
def doprob():
op = choice('+-')
#用choice從'+-'中隨意選擇操作符
nums = [randint(1,10) for i in range(2)]
#用randint(1,10)隨機生成一個1到10的數(shù),隨機兩次使用range(2)
nums.sort(reverse=True)
#按升序排序
ans = ops[op](*nums)
#利用函數(shù)
pr = '%d %s %d = ' % (nums[0], op, nums[1])
oops = 0
#oops用來計算failure測試,當(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):
break
if __name__ == '__main__':
main()
運行結(jié)果如下:
8 - 1 = 7
correct
Again? [y]y
7 - 1 = 6
correct
Again? [y]y
9 + 4 = 0
incorrect... try again
9 + 4 =
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
-
淺談JupyterNotebook導(dǎo)出pdf解決中文的問題
這篇文章主要介紹了淺談JupyterNotebook導(dǎo)出pdf解決中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 2020-04-04
-
Python實現(xiàn)簡單的多任務(wù)mysql轉(zhuǎn)xml的方法
這篇文章主要介紹了Python實現(xiàn)簡單的多任務(wù)mysql轉(zhuǎn)xml的方法,結(jié)合實例形式分析了Python查詢mysql結(jié)果集轉(zhuǎn)xml格式數(shù)據(jù)輸出的相關(guān)操作技巧,需要的朋友可以參考下 2017-02-02
-
詳解Python網(wǎng)絡(luò)爬蟲功能的基本寫法
這篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲功能的基本寫法,網(wǎng)絡(luò)爬蟲,即Web Spider,是一個很形象的名字。把互聯(lián)網(wǎng)比喻成一個蜘蛛網(wǎng),那么Spider就是在網(wǎng)上爬來爬去的蜘蛛,對網(wǎng)絡(luò)爬蟲感興趣的朋友可以參考本文 2016-01-01
最新評論
本文實例講述了Python實現(xiàn)的簡單算術(shù)游戲。分享給大家供大家參考。具體實現(xiàn)方法如下:
#!/usr/bin/env python from operator import add, sub from random import randint, choice ops = {'+': add, '-':sub} #定義一個字典 MAXTRIES = 2 def doprob(): op = choice('+-') #用choice從'+-'中隨意選擇操作符 nums = [randint(1,10) for i in range(2)] #用randint(1,10)隨機生成一個1到10的數(shù),隨機兩次使用range(2) nums.sort(reverse=True) #按升序排序 ans = ops[op](*nums) #利用函數(shù) pr = '%d %s %d = ' % (nums[0], op, nums[1]) oops = 0 #oops用來計算failure測試,當(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): break if __name__ == '__main__': main()
運行結(jié)果如下:
8 - 1 = 7 correct Again? [y]y 7 - 1 = 6 correct Again? [y]y 9 + 4 = 0 incorrect... try again 9 + 4 =
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
淺談JupyterNotebook導(dǎo)出pdf解決中文的問題
這篇文章主要介紹了淺談JupyterNotebook導(dǎo)出pdf解決中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實現(xiàn)簡單的多任務(wù)mysql轉(zhuǎn)xml的方法
這篇文章主要介紹了Python實現(xiàn)簡單的多任務(wù)mysql轉(zhuǎn)xml的方法,結(jié)合實例形式分析了Python查詢mysql結(jié)果集轉(zhuǎn)xml格式數(shù)據(jù)輸出的相關(guān)操作技巧,需要的朋友可以參考下2017-02-02詳解Python網(wǎng)絡(luò)爬蟲功能的基本寫法
這篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲功能的基本寫法,網(wǎng)絡(luò)爬蟲,即Web Spider,是一個很形象的名字。把互聯(lián)網(wǎng)比喻成一個蜘蛛網(wǎng),那么Spider就是在網(wǎng)上爬來爬去的蜘蛛,對網(wǎng)絡(luò)爬蟲感興趣的朋友可以參考本文2016-01-01