python基礎小程序
猜大小的遊戲
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
s = int(random.uniform(1,100))
#print(s)
m = 1
i = 0
while m != s:
m = int(input('輸入你猜的整數:'))
i += 1
if m > s:
print('你猜的數字大了,不要灰心,繼續努力')
if m < s:
print('你猜的數字小了,不要灰心,繼續加油')
if m == s:
if i <= 4:
print('這麽快就猜對了,太神了')
elif i == 5:
print('5次就猜對了,果然厲害')
else :
print i,"次總算猜對了,恭喜恭喜"
break;
===============================
===============================
猜拳小遊戲
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
while 1:
s = int(random.randint(1, 3))
if s == 1:
ind = "石頭"
elif s == 2:
ind = "剪子"
elif s == 3:
ind = "布"
m = raw_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 +",你輸了!"
===============================
===============================
搖篩子遊戲
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import sys
import time
result = []
while True:
result.append(int(random.uniform(1,7)))
result.append(int(random.uniform(1,7)))
result.append(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( 1 ) # 睡眠一秒
break
else:
sys.stdout.write(pointStr + " -> " + "大" + "\n")
time.sleep( 1 ) # 睡眠一秒
break
result = []
===============================
===============================
九九乘法表
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#九九乘法表
i = 1
while i :
j = 1
while j:
print j ,"*", i ," = " , i * j , ' ',
if i == j :
break
j += 1
if j >= 10:
break
print "\n"
i += 1
if i >= 10:
break
===============================
===============================
去除字符串首尾的空格:
def trim(s):
while s[:1] == ' ':
s = s[1:]
while s[-1:] == ' ':
s = s[:-1]
return s
str = ' Runoob '
print(trim(str))
===============================
===============================
十進制轉二進制
#!/usr/bin/python
# -*- coding: UTF-8 -*-
denum = input("輸入十進制數:")
print denum,"(10)",
binnum = []
# 二進制數
while denum > 0:
binnum.append(str(denum % 2)) # 棧壓入
denum //= 2
print '= ',
while len(binnum)>0:
import sys
sys.stdout.write(binnum.pop()) # 無空格輸出print ' (2)'
==============================
==============================
判斷質數
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for num in range(2,100): # 叠代 10 到 20 之間的數字
for i in range(2,num): # 根據因子叠代
if num%i == 0: # 確定第一個因子
j=num/i # 計算第二個因子
# print '%d 等於 %d * %d' % (num,i,j)
break # 跳出當前循環
else: # 循環的 else 部分
print num, '是一個質數'
==============================
=============================
# 打印空心等邊三角形
#!/usr/bin/python
# -*- coding: UTF-8 -*-
rows = int(raw_input('輸入行數:'))
for i in range(0, rows):
for k in range(0, 2 * rows - 1):
if (i != rows - 1) and (k == rows - i - 1 or k == rows + i - 1):
print " * ",
elif i == rows - 1:
if k % 2 == 0:
print " * ",
else:
print " ",
else:
print " ",
print "\n"
==============================
冒泡排序
#!/usr/bin/python
# -*- coding: UTF-8 -*-
arays = [1,5,2,7,6,15,12,9]
for i in range(len(arays)):
for j in range(i+1):
if arays[i] < arays[j]:
# 實現連個變量的互換
arays[i],arays[j] = arays[j],arays[i]
print arays
==========================
==========================
打印菱形
#!/usr/bin/python
# -*- coding: UTF-8 -*-
width = int(5)
for row in range(width + 1):
for col in range(width + 1):
if ((abs(row - width/2) + abs(col - width/2)) == width/2):
print "*",
else:
print " ",
print " "
====================================
python基礎小程序