Python第一周習題集(二)

2022-08-18 06:06:12 字數 3392 閱讀 5512

#

比較大小

#方法1

from math import

maxa = ('

請輸入第乙個整數:')

b = ('

請輸入第二個整數:')

c = ('

請輸入第三個整數:')

d =max(a, b, c)

print('

其中最大的是%f:

' %d)

#

如何交換兩個變數值 a, b = b, a

'''temp = a

a = b

b = temp

'''a = int(input('

a ='

))b = int(input('

b ='

))c = int(input('

c ='

))'''

if a > b:

a, b = b, a

if b > c:

b, c = c, b

if a > b:

a, b = b, a

print(a, b , c)

'''(a, b) = a > b and (b, a) or

(a, b)

(b, c) = b > c and (c, b) or

(b, c)

(a, b) = a > b and (b, a) or

(a, b)

print(a, b, c)

#

判斷乙個數是否是質數

a = int(input('

請輸入乙個數:'))

is_prime =true

for b in range(2, a):

c = a %b

if c ==0:

is_prime =false

break

ifis_prime:

print('

%d他是質數!

' %a)

else

:

print('

%d他不是質數

' % a)

#

九九乘法表

for row in range(1, 10):

for col in range(1, row + 1):

print('

%d*%d=%d

' % (row, col, row * col), end='

\t')#

製表符print()#

換行用,

#

百元百雞,公雞5元乙隻,母雞3元乙隻,小雞一元3只,問有一百元,買一百隻雞,有幾種買法

for x in range(21):

for y in range(34):

#for z in range(0, 100, 3):

z = 100 - x -y

if 5 * x + 3 * y + z // 3 == 100 and z % 3 ==0:

print(x, y ,z)

print(0.1 + 0.2 + 0.3)

print(0.3 + 0.2 + 0.1)

#由於浮點數表示法的問題,

#在實際開發的過程中請不要做浮點數的==和!=運算.

#實在要用,一定要先轉換成int再計算,算完再轉換回去.

#

找出 1000 以內的水仙花數。

from math import

powfor num in range(1000):

a = num // 100

b = num // 10 % 10c = num % 10

if num == pow(a, 3) + pow(b, 3) + pow(c, 3):

print('

%f是水仙花數!

' %num)

#

完美數from math import

sqrt

for num in range(10000):

sum =0

for i in range(2, int(sqrt(num)) + 1):

if num % i ==0:

sum +=i

if i != num //i:

sum += num //i

if num == sun + 1:

print('

%d是完美數

' %num)

#

經典的craps賭博遊戲

from random import

randint

go_on =false

num1 = randint(1, 6)

num2 = randint(1, 6)

total1 = num1 +num2

print('

玩家搖出了%d

' %total1)

if total1 == 7 or total1 == 11:

print('

玩家勝'

)elif total1 == 2 or total1 == 3 or total1 == 12:

print('

玩家輸'

)else

: go_on =true

while

go_on:

num1 = randint(1, 6)

num2 = randint(1, 6)

total2 = num1 +num2

print('

玩家搖出了%d

' %total2)

if total2 ==total1:

print('

玩家勝!')

go_on =false

elif total2 == 7:

print('

玩家輸!')

go_on = false

#

經典的五人分魚問題

fish = 1

while

true:

totle =fish

is_enough =true

for _ in range(5):

if (total - 1) % 5 ==0:

total = (total - 1) // 5 * 4

else

:

break

is_enough =false

ifis_enough:

print

(fish)

break

fish += 1

python第一周心得 Python第一周總結

變數 不能為系統自帶的內建函式,如def,help,sum等等 變數不能已下劃線數字來開頭,對大小寫敏感 變數後直接接數字,則被賦值為整型,如何加引號則賦值型別為字串str型別 a 1 整型 int b 2 字串 str 用print 直接輸出 a hello print a 在 使用者想要同時輸出...

python 一 第一周

print hello word 列印hello word name 字串 int 1boo trueusername input username 輸入 print username info info format username username print info username na...

Python第一周總結

import this命令可以輸出它的具體內容,在初學python時可以讓我們對python這門語言的特性有乙個大體的了解,對今後的程式設計提供了很大的幫助 定義乙個python變數可以使用數字 字母 下劃線數字不能開頭,不能使用特殊字元和空格,變數的型別有整形 浮點型 字串型分別對應int flo...