原文見我的個人部落格
大學經常組織你畫我猜遊戲,這次學習python正好寫了乙個遊戲自動發題和記分,只需要維護題庫就行。直接上原始碼tinygame.py
。
執行時用
python3 -u tinygame.py
執行
import time
import random
def changepoint(playerpoints: dict):
str = input('輸入參與者得分情況(add(sub) name point)')
cmd = str.split()
if(cmd[1] in playerpoints):
if(cmd[0] == 'add'):
playerpoints[cmd[1]] += int(cmd[2])
print(cmd[1], '增加', cmd[2], '分\n')
elif(cmd[0] == 'sub'):
playerpoints[cmd[1]] -= int(cmd[2])
print(cmd[1], '減少', cmd[2], '分\n')
else:
print('參與者姓名不存在!請重新輸入\n')
def showplayerpoints(playerpoints: dict):
print('姓名\t得分')
for p in playerpoints:
print(p, '\t', playerpoints[p])
print()
def paintandguess(playerpoints: dict, orinarr: list):
print('************歡迎來到你畫我猜遊戲************')
probnum = int(input('現在請輸入每組的題量'))
probnum = probnum * len(playerpoints)
if(probnum > len(orinarr)):
print('題庫題量不足!當前僅剩', len(orinarr), '題')
else:
arr = random.sample(orinarr, probnum)
for a in arr:
orinarr.remove(a)
while true:
print('輸入start開始')
if(input() == 'start'):
break
for a in arr:
\# print(a)
for i in range(len(a)):
print(a[i], end=' ')
time.sleep(0.5)
print()
while true:
print('輸入回車顯示下一題')
if(input() == ''):
break
return orinarr
def printcmds():
print('命令\t\t作用')
print('show\t\t檢視積分列表')
print('modpoint\t增加或減少積分')
print('pag\t\t你畫我猜遊戲')
print('help\t\t檢視所有命令和用法')
print('endgame\t\t退出程式')
print('\n')
playernum = int(input('please input the num of players\n'))
print()
players = [n for n in range(playernum)]
playerpoints = {}
for i in range(playernum):
print(i+1, 'player name')
players[i] = input()
playerpoints[players[i]] = 0
print('\n')
file1 = open('./paintandguess.txt')
orinarr = file1.read().split('\n')
orinarr.remove('')
file1.close
while true:
print('請輸入要做的事,不知道要做什麼請輸入help')
cmd = input()
if(cmd == 'endgame'):
break
elif(cmd == 'modpoint'):
changepoint(playerpoints)
elif(cmd == 'help'):
printcmds()
elif(cmd == 'pag'):
orinarr = paintandguess(playerpoints, orinarr)
elif(cmd == 'show'):
showplayerpoints(playerpoints)
else:
print('命令不存在!您可以用以下合法命令')
printcmds()
print('請多多支援我的個人部落格:')
print('也請多多支援我的csdn部落格:')
print('輸入回車退出............')
while true:
if(input() == ''):
break
python實現小程式 python實現小程式
1 楊輝三角形python實現 1 11 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 楊輝三角形 def triangel n l 1 定義乙個list 1 while true yield l 列印出該list l l x l x 1 for x in range le...
Python小程式 實現簡單的猜拳遊戲
import random import time defgame play point 0 computer point 0for i in range 3 print 這是第 局 format i 1 time.sleep 1 player choice input 請輸入你的選擇 石頭 剪刀 ...
python實現小程式
1 楊輝三角形python實現 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 楊輝三角形 def triangel n l 1 定義乙個list 1 while true yield l 列印出該list l l x l x 1 for x in range...