#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 调用15-1模块
##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函数 动手试一试
#15.1 编写一个函数,用大写字母打印你的名字。 保存为y1511.py 到博客发布后图形会变形。
def printName():
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.printName()
#15.2 不用my_module
from my_module import c_to_f #方法二
celsius = float(input ("Enter aremperature in Celsius:"))
fahrenheit = c_to_f(celsius)
print("That's",fahrenheit,"degrees Fahrenheit")
#15模块 动手试一试
#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())
# 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 调用15-1模块
##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函数 动手试一试
#15.1 编写一个函数,用大写字母打印你的名字。 保存为y1511.py 到博客发布后图形会变形。
def printName():
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.printName()
#15.2 不用my_module
from my_module import c_to_f #方法二
celsius = float(input ("Enter aremperature in Celsius:"))
fahrenheit = c_to_f(celsius)
print("That's",fahrenheit,"degrees Fahrenheit")
#15模块 动手试一试
#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())
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2076965/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/220205/viewspace-2076965/