1. 猜數(shù)字
代碼1:
#!usr/bin/env python
# _*_ coding:utf-8 _*_
# @Author : TianYu
# @Time : 2017/9/18 19:39
# @File : 草稿本.py
import random
s = int(random.uniform(1,10))#隨機生成下一個實數(shù),它在[a,b]范圍內(nèi)(左閉右開)
m = int(input("請輸入整數(shù):"))
while m != s:
if m > s:
print("數(shù)太大了")
m = int(input("請輸入整數(shù):"))
if m < s:
print("數(shù)太小了")
m = int(input("請輸入整數(shù):"))
if m == s:
print("你猜對了")
break
代碼2:
import random
import math
a=random.random() #生成一個(0~1)之間的隨機數(shù)
b=math.floor(a*100+1)#floor又兵,浮點數(shù)的地板,即求一個最接近它的整數(shù)薄翅,它的值小于或等于這個浮點數(shù)钮追。
#print(b)
flag=True
while(flag):
c = float(input("輸出一個數(shù):"))
if b==c:
print('你猜對了')
flag=False
elif b>c:
print('數(shù)太小')
else:
print('數(shù)太大了')
2. 猜拳游戲
代碼:
#!usr/bin/env python3
# _*_ coding:utf-8 _*_
# @Author : TianYu
# @Time : 2017/9/18 19:39
# @File : 草稿本.py
import random
while 1:
s = int(random.randint(1, 3))
if s == 1:
ind = "石頭"
elif s == 2:
ind = "剪子"
elif s == 3:
ind = "布"
m = input('輸入 石頭宦搬、剪子沈自、布,輸入"end"結(jié)束游戲:')
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 +"贰镣,你輸了呜象!")
搖骰子游戲
代碼:
import random
import sys
import time
result = []
#程序一直運行,需手動結(jié)束
while True:
result.append(int(random.uniform(1,7)))#模擬投骰子碑隆,隨機生成1-6中的一個數(shù)
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 ) # 睡眠一秒
else:
sys.stdout.write(pointStr + " -> " + "大" + "\n")
time.sleep( 1 ) # 睡眠一秒
result = []
十進制轉(zhuǎn)換為二進制
代碼:
import sys
denum = int(input("輸入十進制數(shù):"))
print("輸入的十進制數(shù)為:\n",denum)
binnum = []#用來存放二進制位
# 二進制數(shù)
while denum > 0:
binnum.append(str(denum % 2)) # 棧壓入
denum //= 2
print("轉(zhuǎn)換為二進制為:")
while len(binnum)>0:
sys.stdout.write( binnum.pop()) # 無空格輸出二進制數(shù)
九九乘法表
代碼1.
for a in range(1,10):
for b in range(1,a+1):
print("%d*%d=%2d" % (a,b,a*b),end=' ')
print('')
代碼2.
#輸出樣式多樣
for a in range(10):
s = ""
for b in range(1,a+1):
s += str(a)+ "*" + str(b) + "="+ str(a*b) + " "
#print(s)
print(s.center(69," "))# 居中
#print(s.ljust(69," ")) # 左對齊
# print(s.rjust(69," "))# 右對齊
要努力要奮斗