Python練手小習題

2021-08-21 03:03:22 字數 2776 閱讀 8109

import random

s =int

(random.uniform(1,

1000))

m =int

(input

('輸入整數:'))

while m != s:

if m > s:

print

('大了'

) m =

int(

input

('輸入整數:'))

if m < s:

print

('小了'

) m =

int(

input

('輸入整數:'))

if m == s:

print

('ok'

)break

;

2. python實現與使用者互動完成「石頭剪刀布遊戲」

import random

while1:

s =int(random.randint(1,

3))if s ==1:

ind =

"石頭"

elif s ==2:

ind =

"剪子"

elif s ==3:

ind =

"布" m =

input

('輸入 石頭、剪子、布,輸入"end"結束遊戲:'

) blist =

['石頭'

,"剪子"

,"布"]if

(m not

in blist)

and(m !=

'end'):

print

("輸入錯誤,請重新輸入!"

)elif

(m not

in blist)

and(m ==

'end'):

print

("\n遊戲退出中..."

)break

elif m == ind :

print

("電腦出了: "

+ ind +

",平局!"

)elif

(m ==

'石頭'

and ind ==

'剪子')or

(m ==

'剪子'

and ind ==

'布')

or(m ==

'布'and ind ==

'石頭'):

print

("電腦出了: "

+ ind +

",你贏了!"

)elif

(m ==

'石頭'

and ind ==

'布')

or(m ==

'剪子'

and ind ==

'石頭')or

(m ==

'布'and ind ==

'剪子'):

print

("電腦出了: "

+ ind +

",你輸了!"

)

3. python實現」搖篩子遊戲「

import random

import sys

import time

result =

while

true

:int

(random.uniform(1,

7)))

int(random.uniform(1,

7)))

int(random.uniform(1,

7)))

print

(result)

count =

0 index =

2 pointstr =

""while index >=0:

currpoint = result[index]

count += currpoint

index -=

1 pointstr +=

" " pointstr +=

str(currpoint)

if count <=11:

sys.stdout.write(pointstr +

" -> "

+"小"

+"\n"

) time.sleep(2)

# 睡眠2秒

else

: sys.stdout.write(pointstr +

" -> "

+"大"

+"\n"

) time.sleep(2)

# 睡眠2秒

result =

4. python實現」九九乘法表「

i=

1while i:

j=1while j:

print

(j,"*"

,i,"="

,i*j,

" ",

)if i==j:

break

j+=1if j>=10:

break

print

("\n"

) i+=

1if i>=10:

break

python練手小程式

python小白 usr bin env python coding utf 8 time 2019 11 5 8 53 author october file py 整數序列求和 n input 請輸入整數n sum 0 for i in range int n sum i 1print 1到n求...

10個Python練手小程式

程式1 題目 有1 2 3 4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?1.程式分析 可填在百位 十位 個位的數字都是1 2 3 4。組成所有的排列後再去 掉不滿足條件的排列。2.程式源 for i in range 1,5 for j in range 1,5 for k in ...

python練手題目 Python練手題目(七)

1.計算重複字母出現的次數 編寫乙個函式,該函式將返回在輸入字串 現多次 不同的不區分大小寫的 字母字元和數字的計數。可以假定輸入字串僅包含字母 大寫和小寫 和數字。例如 abcde 0 no characters repeats more than once aabbcde 2 a and b a...