Python程式設計if語句 真實案例

2021-10-02 00:04:26 字數 649 閱讀 4151

剪刀石頭布

#匯入隨機工具包

import random

player=int(input("請輸入剪刀(1)/剪刀(2)/布(3)"))

computer = random.randint(1, 3)

print("玩家選擇的是 %d - 電腦出的是%d" % (player,computer))

if((player == 1 and computer == 2)

or (player == 2 and computer == 3)

or(player == 3 and computer == 1)):

print("電腦弱爆,玩家勝利")

elif player == computer :

print("平局了")

else :

print("電腦勝利了")

知識點:

1、if  的條件語句比較多的時候,可以用換行8個空格優化一下條件語句的結構;注意和執行語句要有一行空格隔開。

2、import 可以匯入隨機工具包,找到乙個適合的工具random.randint(),來進行生成隨機數。

3、random.randint(a,b)  的用法, 生成乙個x,a<= x <= b  且注意,a<=b

python程式設計的條件語句 Python 條件語句

python 條件語句 python條件語句是通過一條或多條語句的執行結果 true或者false 來決定執行的 塊。可以通過下圖來簡單了解條件語句的執行過程 python程式語言指定任何非0和非空 null 值為true,0 或者 null為false。python 程式設計中 if 語句用於控制...

demo39 案例 盒子的真實尺寸

doctype html html head meta charset utf 8 title title style type text css box span style head body div class title h2 align left html概述 h2 div div cla...

Python程式設計 檔案讀寫操作(with 語句)

with語句相比以往的讀寫檔案,能自動幫我們呼叫close 方法,避免浪費系統的資源。1.1 向檔案中寫內容 語法如下 with open 1.txt w encoding utf 8 as f f.write 向1.txt檔案中寫入文字 with open 1.txt w encoding utf...