有人在广场安装了一个数字炸弹,需要人为去猜测炸弹数字,总共有6次机会,6次机会一用完,如果没有猜中则炸弹立马爆炸:
import random
y = random.randint(1, 100)
min = 1
max = 100
count = 6
print('开始拆炸弹吧!您共有%d次机会' % count)
while count > 0:
print('请输入一个', min, '-', max, '之间的数:')
x = int(input())
if x == y:
print('恭喜你,炸弹解除!')
break
elif x < y:
count -= 1
if count == 0:
print('嘭!炸弹炸了,数字是', y)
elif count == 1:
print('可惜,数字猜小了,数字在', x, '-', max, '之间,最后一次机会了,好好把握呀!')
else:
print('可惜,数字猜小了,数字在', x, '-', max, '之间,再猜一猜,你还有%d次机会。' % count)
min = x
else:
count -= 1
if count == 0:
print('嘭!炸弹炸了,数字是', y)
elif count == 1:
print('可惜,数字猜大了,数字在', min, '-', x, '之间,最后一次机会了,好好把握呀!')
else:
print('可惜,数字猜大了,数字在', min, '-', x, '之间,再猜一猜,你还有%d次机会。' % count)
max = x
家里有小朋友总是拉着你跟他们玩小游戏,比如石头剪刀布,所以我就用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('电脑获胜')
全部0条评论
快来发表一下你的评论吧 !