邏輯判斷和條件控制語句作業

2021-10-09 15:12:57 字數 2362 閱讀 5700

1.列印菱形

import math

i = 1

#設定列印的行數

s = int(input('請輸入列印的行數:'))

2.

#2.求1000以內的水仙花數;

list =

for num in range(0,1000):

#百位用a,十位用b,個位用a;

3.

#3.使用者隨意輸入乙個數,判斷其是否為質數:

a = int(input('請隨意輸入乙個數:'))

i = 2

n = 0

if a > i:

while a>i:

if a % i == 0:

n +=1

i+=1

break

else:

i += 1

if n !=0:

print('您輸入的數不是質數')

else:

print('你輸入的是質數')

elif a <= 0:

print('請輸入正確的質數')

else:

print('你輸入的是質數')

4.

#3.猜拳遊戲: 出拳:玩家:手動輸入 電腦:隨機輸入  判斷:玩家獲勝,電腦獲勝,平局;

import random

print("""-----------

歡迎進入猜拳小遊戲

----------""")

# 玩家的選擇

s1 = input('請輸入您想出的:')

# 電腦的選擇

s = random.randint(1,3)

def copu(s):

if s == 1:

s2 = '石頭'

elif s == 2:

s2 = '剪刀'

else:

s2 = '布'

return s2

s3 = copu(s)

if s1 == '石頭' and s3 == '剪刀':

print('你太厲害了,你贏了!,你出的{},電腦出的{}'.format(s1,s3))

elif s1 == '石頭' and s3 == '布':

print('有點可惜,你輸了!,你出的{},電腦出的{}'.format(s1, s3))

elif s1 == '剪刀' and s3 == '石頭':

print('有點可惜,你輸了!,你出的{},電腦出的{}'.format(s1, s3))

elif s1 == '剪刀' and s3 == '布':

print('你太厲害了,你贏了!,你出的{},電腦出的{}'.format(s1, s3))

elif s1 == '布' and s3 == '石頭':

print('你太厲害了,你贏了!,你出的{},電腦出的{}'.format(s1, s3))

elif s1 == '布' and s3 == '剪刀':

print('有點可惜,你輸了!,你出的{},電腦出的{}'.format(s1, s3))

elif s1 == s3:

print('平局!,你出的{},電腦出的{}'.format(s1, s3))

else:

print('輸入有誤,請重新輸入')

MySql儲存過程 5 邏輯判斷,條件控制

同編寫程式類似,儲存過程中也有對應的條件判斷,功能類似於if switch。在mysql裡面對應的是if和case 1 if判斷 if判斷的格式是這樣的 if expression then commands elseif expression then commands else commands...

條件控制語句

if 條件表示式 塊 塊中儲存著 組 同 個 塊中的 要麼都執 要麼都不執 塊以縮排開始,直到 恢復到之前的縮排級別時結束 塊就是 種為 分組的機制 num int input 可以將input 的返回值轉換為int型別if 條件表示式 塊else 塊 if 條件表示式 塊elif 條件表示式 塊e...

條件控制語句

2.input 函式 3.if else語句 4.if elif else語句 5.while 條件運算子可以將語句與語句連線,用於邏輯判斷。形如下 語句1if表示式else語句2 如果表示式的判斷結果為true,則會執行語句1,反之,則會執行語句2。and比or的優先順序高。通過語句m 2 or ...