第三天,小作業,表示式,while迴圈

2021-10-06 19:40:35 字數 4292 閱讀 4742

num += 1 等價於 num = num + 1

num -= 1 等價於 num = num - 1

num *= 2 等價於 num = num * 2

num /= 2 等價於 num = num / 2

num //= 2 等價於 num = num // 2

num %= 2 等價於 num = num % 2

num **= 2 等價於 num = num ** 2

##輸入3個數,比較數值大小並列印

#第一種

num1 = int(input(「num1:」))

num2 = int(input(「num2:」))

num3 = int(input(「num3:」))

max_num =0

if num1>num2:

max_num= num1

if max_num > num3:

print(「max num is」,max_num)

else:

print(「max num is」,num3)

else:

max_num = num2

if max_num > num3:

print(「max num is」,max_num)

else:

print(「max num is」,num3)

##第二種

num1=int(input(「num1:」))

num2=int(input(「num2:」))

num3=int(input(「num3:」))

a=num1-num2

b=num1-num3

c=num2-num3

if a>=0 and b>=0:

print(「the max number is」,「num1:」,num1)

elif a<=0 and c>=0:

print(「the max number is」,「num2:」,num2)

else :

print(「the max number is」,「num3:」,num3)

if a<=0 and b<=0:

print(「the min number is」,「num1:」,num1)

elif a>=0 and c<=0:

print(「the min number is」,「num2:」,num2)

else :

print(「the min number is」,「num3:」,num3)

##while迴圈 語句

格式:while 條件:

命令##while語句當條件為真時執行

『』'age=50

flag=true ####–錯誤—,真值賦值首字母必須大寫 flag=true

while flag=true: ####–錯誤—,while迴圈判斷條件是判斷真假,不用等式,應該為while flag:

user_age=int(input(「guess my age:」))

if user_age=age: ###—錯誤—判斷中==表示等於

print (「yes」)

flag=false ####–錯誤—,真值賦值首字母必須大寫 ,flag=false

elif user_ageprint(「pls try bigger.」)

else:

print(「pls try smaller.」)

print(「end」)』』』

####while語句 執行命令需要首行縮排,不能在寫字本上寫**!!!縮排符格式不一樣,報錯"invalid character indentifier"

num=1

while num<=10:

num+=1

if num==11: ##while中加入if判斷,if條件成立,continue 跳過後面的while迴圈語句.continue表示返回到while迴圈開始的地方.

continue

print(num)

##輸出結果2 3 4 5 6 7 8 9 10 沒有11

###while…:

else:

####表示如果while迴圈正常結束,則執行else後面的語句,continue不會破壞while迴圈.非正常結束為break或者報錯

print(「hello world.」,end=』』)

print(「hello world.」,end=』』)

print(「hello world.」,end=』』)

#####輸出3行變為1行 end=』』

##九九乘法表

a=1b=1

while a<=9:

c=a*b

print(a,『x』,b,』=』,c,end=』 『)

b+=1

if b==10:

print(』』)

a+=1

b=acontinue

1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9

2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18

3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27

4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36

5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45

6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54

7 x 7 = 49 7 x 8 = 56 7 x 9 = 63

8 x 8 = 64 8 x 9 = 72

9 x 9 = 81

###錯誤示範

a=1b=1

while a<=9:

c=a*b

print(a,『x』,b,』=』,c,end=』 『)

b+=1

if b==9:

print(』』)

a+=1

b=acontinue ########此程式在執行時,a=9以後,b不停累加,b>9,不會執行if巢狀,不執行a累加,則迴圈無限重複.

1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8

2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16

3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24

4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32

5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40

6 x 6 = 36 6 x 7 = 42 6 x 8 = 48

7 x 7 = 49 7 x 8 = 56

8 x 8 = 64

9 x 9 = 81 9 x 10 = 90 9 x 11 = 99 9 x 12 = 108 9 x 13 = 117 9 x 14 = 126 9 x 15 = 135 9 x 16 = 144 9 x 17 = 153 9 x 18 = 162 9 x 19 = 171 9 x 20 = 180 9 x 21 = 189 9 x 22 = 198 9 x 23 = 207 9 x 24 = 216 9 x 25 = 225 9 x 26 = 234 9 x 27 = 243 9 x 28 = 252 9 x 29 = 261 9 x 30 = 270 9 x 31 = 279 9 x 32traceback (most recent call last):

file 「d:/wpy64-3720/zz/0524-3.py」, line 6, in

print(a,『x』,b,』=』,c,end=』 ')

keyboardinterrupt

###金字塔形

a=1b=1

while b<=9:

c=ab

print(a,』』,b,』=』,c,end=』\t』)

a+=1

while a>b:

print()

b+=1

a=1

Prolog第三天作業

1.解決6x6 與9x9數獨問題。擴充套件原例子4x4的數獨,重複寫行 列 格仔太麻煩了,直接貼4x4的例子 use module library clpfd valid valid head tail all different head valid tail sudoku puzzle,solu...

2 第三天作業

1.通過檢視時間命令將分別將年月日時分寫入a檔案,將當前顯示為星期幾追加寫 入a檔案。或者在螢幕上輸出 the day is 星期幾 解答 root localhost date cut d f 1 5 a.txt root localhost cat a.txt sat feb 27 07 20 ...

第三天python作業

name alex lenb name b name.strip print name b name alex lenb name b name.replace l p 1 print name b name alex lenb name b name.split l print name b na...