有人在廣場安裝了一個數(shù)字炸彈,需要人為去猜測炸彈數(shù)字,總共有6次機會,6次機會一用完,如果沒有猜中則炸彈立馬爆炸:
import random
y = random.randint(1, 100)
min = 1
max = 100
count = 6
print('開始拆炸彈吧!您共有%d次機會' % count)
while count > 0:
print('請輸入一個', min, '-', max, '之間的數(shù):')
x = int(input())
if x == y:
print('恭喜你,炸彈解除!')
break
elif x < y:
count -= 1
if count == 0:
print('嘭!炸彈炸了,數(shù)字是', y)
elif count == 1:
print('可惜,數(shù)字猜小了,數(shù)字在', x, '-', max, '之間,最后一次機會了,好好把握呀!')
else:
print('可惜,數(shù)字猜小了,數(shù)字在', x, '-', max, '之間,再猜一猜,你還有%d次機會。' % count)
min = x
else:
count -= 1
if count == 0:
print('嘭!炸彈炸了,數(shù)字是', y)
elif count == 1:
print('可惜,數(shù)字猜大了,數(shù)字在', min, '-', x, '之間,最后一次機會了,好好把握呀!')
else:
print('可惜,數(shù)字猜大了,數(shù)字在', min, '-', x, '之間,再猜一猜,你還有%d次機會。' % count)
max = x
3 石頭剪刀布
家里有小朋友總是拉著你跟他們玩小游戲,比如石頭剪刀布,所以我就用Python寫了一個簡易小程序,讓電腦和小朋友玩,這樣我就可以自己耍了。
游戲是三局兩勝制,且看:
import random
choose = {1:'石頭', 2:'剪刀', 3:'布'}
print('本游戲采用三局兩勝制')
times = 3
computer_flag = 0
player_flag = 0
for i in range(1,times+1):
print('\\n')
print('第%d局' % i)
player = int(input('玩家請出拳:石頭(1)、剪刀(2)、布(3):'))
computer = random.randint(1, 3)
player_choose = choose[player]
computer_choose = choose[computer]
if player == computer:
print(f'您出{player_choose},電腦出{computer_choose}\\n此局平局')
computer_flag += 0
player_flag += 0
print('目前,玩家贏%d局,電腦贏%d局' % (player_flag, computer_flag))
elif player == 1 and computer == 2 or player == 2 and computer == 3 or player == 3 and computer == 1:
print(f'您出{player_choose},電腦出{computer_choose}\\n此局你贏了')
computer_flag += 0
player_flag += 1
print('目前,玩家贏%d局,電腦贏%d局' % (player_flag, computer_flag))
else:
print(f'您出{player_choose},電腦出{computer_choose}\\n此局你輸了')
computer_flag += 1
player_flag += 0
print('目前,玩家贏%d局,電腦贏%d局' % (player_flag, computer_flag))
print('=========================================')
if player_flag==computer_flag:
print("平局,無獲勝者")
elif player_flag > computer_flag:
print('玩家獲勝')
else:
print('電腦獲勝')
-
編碼
+關(guān)注
關(guān)注
6文章
952瀏覽量
54891 -
代碼
+關(guān)注
關(guān)注
30文章
4813瀏覽量
68833 -
python
+關(guān)注
關(guān)注
56文章
4802瀏覽量
84890
發(fā)布評論請先 登錄
相關(guān)推薦
評論