#代码清单15-1创建一个模块
# this is the file "my_module.py"
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 /5 +32
return fahrenheit
#代码清单15-2 使用模块
import my_module #方法一
##from my_module import c_to_f #方法二
celsius = float(input ("Enter aremperature in Celsius:"))
fahrenheit = my_module.c_to_f(celsius) #方法一
##fahrenheit = c_to_f(celsius) #方法二
print("That's",fahrenheit,"degrees Fahrenheit")
#代码清单15-3让程序睡眠
import time
print ("How",end=" ")
time.sleep(1)
print("are",end=" ")
time.sleep(1)
print("you",end=" ")
time.sleep(1)
print("today?")
##随机数
import random
print(random.randint(0,100))
print(random.randint(0,100))
print(random.random()) #0-1的随机小数
print(random.random()*10)
from time import sleep
print("Hello,talk to you again in 2 seconds...")
sleep(2)
print("Hi again")
#15.1 编写一个函数,用大写字母打印你的名字。 保存为y1511.py 到博客发布后图形会变形
def printNameBig():
print(" CCCC A RRRRR TTTTTTT EEEEEE RRRRR")
print(" C C A A R R T E R R")
print("C A A R R T EEEEEE R R")
print(" C AAAAAAAAA RRRRRR T E RRRRRR")
print(" C CA A R R T E R R")
print(" CCCC A AR R T EEEEEE R R")
print()
#调用
import y1511
y1511.printNameBig()
#from y1511 import *
#printNameBig()
#15.2修改代码清单15-2 使用模块
import my_module #方法一
##from my_module import c_to_f #方法二
celsius = float(input ("Enter aremperature in Celsius:"))
fahrenheit = my_module.c_to_f(celsius) #方法一
##fahrenheit = c_to_f(celsius) #方法二
print("That's",fahrenheit,"degrees Fahrenheit")
#15.3 编写一个程序,打印1,20之间五个随机整数的列表
#方法一:
import random
def randomprint():
print(random.randint(1,20))
for i in range(5):
randomprint()#调用模块函数
#方法二:
for i in range(5):
print(random.randint(1,20))
#15.4 编写一个小程序,要求他工作30秒,每三秒打印一个随机数
#方法一:
#取名字为y1533.py
import random
def randomprint1():
print(random.random())
#再建立一个py文件
import time
import y1533
for i in range(1,11):
time.sleep(3)
print("第",i,"次","第",i*3,"秒","随机打印一个小数:",end="")
y1533.randomprint1()
#方法二:
import time,random
for i in range(10):
time.sleep(3)
print(random.random())