Python實現(xiàn)的簡單算術(shù)游戲?qū)嵗?/h1>
更新時間:2015年05月26日 10:16:01 作者:buaa_shang
這篇文章主要介紹了Python實現(xiàn)的簡單算術(shù)游戲,可實現(xiàn)隨機(jī)給出算數(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)隨機(jī)生成一個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用來計算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()
運(yùn)行結(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)文章
-
Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例
這篇文章主要介紹了Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧 2020-02-02
-
Python可以實現(xiàn)棧的結(jié)構(gòu)嗎
在本篇文章里小編給各位整理的是關(guān)于Python實現(xiàn)棧的結(jié)構(gòu)的條件的相關(guān)知識點,有需要的朋友們可以學(xué)習(xí)下。 2020-05-05
-
python獲取一組數(shù)據(jù)里最大值max函數(shù)用法實例
這篇文章主要介紹了python獲取一組數(shù)據(jù)里最大值max函數(shù)用法,實例分析了max函數(shù)的使用技巧,需要的朋友可以參考下 2015-05-05
-
Django?ORM?事務(wù)和查詢優(yōu)化的操作方法
這篇文章主要介紹了Django?ORM?事務(wù)和查詢優(yōu)化,包括事務(wù)操作、ORM 惰性查詢及only與defer相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下 2022-09-09
最新評論
本文實例講述了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)隨機(jī)生成一個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用來計算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()
運(yùn)行結(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)文章
Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例
這篇文章主要介紹了Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python可以實現(xiàn)棧的結(jié)構(gòu)嗎
在本篇文章里小編給各位整理的是關(guān)于Python實現(xiàn)棧的結(jié)構(gòu)的條件的相關(guān)知識點,有需要的朋友們可以學(xué)習(xí)下。2020-05-05
python獲取一組數(shù)據(jù)里最大值max函數(shù)用法實例
這篇文章主要介紹了python獲取一組數(shù)據(jù)里最大值max函數(shù)用法,實例分析了max函數(shù)的使用技巧,需要的朋友可以參考下2015-05-05
Django?ORM?事務(wù)和查詢優(yōu)化的操作方法
這篇文章主要介紹了Django?ORM?事務(wù)和查詢優(yōu)化,包括事務(wù)操作、ORM 惰性查詢及only與defer相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09

