猜拳小遊戲(python)

2021-09-01 02:52:57 字數 1018 閱讀 1824

import random						#載入模組random(隨機數)

win = 0 #定義勝場;

lose = 0 #定義敗場;

dogfall = 0 #定義平局;

while true: #while 迴圈;

print('=' * 10 + '猜拳遊戲' + '=' * 10) #遊戲開頭輸出:遊戲名

print('勝: %s,敗: %s,平: %s' % (win,lose,dogfall)) #記錄對戰結果;

print('1、石頭','2、剪刀','3、布','4、退出') #提示操作,選項;

computer = ('石頭','剪刀','布') #定義電腦迴圈的值;

a = random.choice(computer) #隨機選取乙個,

b = input("請輸入:") #玩家選擇出拳;

if (b == '1' and a == '石頭') or (b == '2' and a == '剪刀') or (b == '3' and a == '布'): #平局結果;

dogfall += 1 #計數平局+1;

print('不要走,決戰到天亮!!')

elif(b == '1' and a == '剪刀') or (b == '2' and a == '布') or (b == '3' and a == '石頭'): #勝利結果;

win += 1

print('好吧,你贏了!')

elif(b == '4'):

break

elif b not in ('1','2','3','4'): #防止輸入其他選項;

print('滾去,重新輸入!')

else:

lose += 1

print('哈哈!你輸了!')

python小遊戲(猜拳)

usr bin python import random choices 石頭 剪刀 布 computer random.choice choices 生成乙個隨機值 print 猜拳遊戲開始.print 請輸入數字 print 1.石頭 2.剪刀 3.布 while true guess num ...

python猜拳小遊戲

import random while true game player input 請輸入 剪刀 0 石頭 1 布 2 退出 3 isdigit 判斷字串是否為數字 if player.isdigit player int player 退出遊戲 if player 3 print 退出成功,感謝...

python猜拳小遊戲

使用if else結構寫的乙個猜拳小遊戲 import random player input 請輸入 剪刀 0 石頭 1 布 2 player int player computer random.randint 0,2 print 電腦出的是 d computer if player0 and ...