第3章 python实战例子 第一节
来源:互联网 发布:黑马python百度云 编辑:程序博客网 时间:2024/06/06 02:57
第3章
1、实战例子
#Craps Roller
#演示随机数的生成
import random #载入随机数模块
#生成1到6之间的随机数
die1 = random.randint(1,6) #randint()函数是用来产生随机数
die2 = random.randrange(6) + 1 #randrange()函数是用来产生一个随机整数,这里加1会得到die2的1-6正确值
total = die1 + die2
print("You rolled a",die1,"and a",die2, "for a total of",total)
print("\n\nPress the enter key to exit.")
2、实战例子
#Password
#演示if语句
print("Welcome to System Security Inc.")
print("-- where security is our middle name\n")
password = input("Enter your password:")
if password == "secret":
#输入正确的密码才能得到以下两行.
print("Access Granted")
print("Welcome! You must be someone very important.")
input("\n\nPress the enter key to exit.")
3、实战例子
#Password
#演示else语句
print("Welcome to System Security Inc.")
print("-- where security is our middle name\n")
password = input("Enter your password:")
if password == "secret":
#输入正确的密码才能得到以下两行.
print("Access Granted")
print("Welcome! You must be someone very important.")
else:
print("Access Denied")
input("\n\nPress the enter key to exit.")
4、实战例子
#Mood Computer
#演示elif子句
import random
print("I sense your energy. Your true emotions are coming across my screen.")
print("You are...")
mood = random.randint(1,3)
if mood == 1:
print("""
╔╗
║╚╗╔═╗╔═╗╔═╗╔╦╗
║║║║═║║║║║║║║║║
╚╩╝╚╩╝║╔╝║╔╝╠═║
╚╝ ╚╝ ╚═╝
""")
elif mood == 2:
print("""
╔╗
╔═╗╔═╗╔═╗╔═╗╔╦╗╔═╗║║
║║║║╩╣║║║║╩╣║╔╝║═║║║
╠╗║╚═╝╚╩╝╚═╝╚╝ ╚╩╝╚╝
╚═╝
""")
elif mood == 3:
print("""
╔══╗
║══╣╔═╗╔╦╗╔╦╗╔╦╗
╠══║║║║║╔╝║╔╝║║║
╚══╝╚═╝╚╝ ╚╝ ╠═║
╚═╝
""")
else:
print("Illegal mood value! (You must be in a really bad mood).")
print("...today.")
input("\n\nPress the enter key to exit.")
5、实战例子
#Three Year-Old Simulator
#演示while循环
print("\tWelcome to the 'three-Year-Old Simulator'\n")
print("This program simulates a conversation with a three-year-old child.")
print("Try to stop the madness.\n")
response = ""
while response != "Because.":
response = input("Why?\n")
print("Oh. Okay.")
input("\n\nPress the enter key to exit.")
6、实战例子
#Losing Battle
#演示可怕的无限循环
print("Your lone hero is surrounded by a massive army of trolls. ")
print("Their decaying green bodies stretch out,melting into the horizon.")
print("Your hero unsheathes his sword for the last fight of his life.\n")
health = 10
trolls = 0
damage = 3
while health != 0:
trolls += 1
health -= damage
print("Your hero swings and defeats an evil troll,"\
"but takes",damage,"damage points.\n")
print("Your hero fought valiantly and defeated",trolls,"trolls.")
print("But alas,your hero is no more.")
7、实战例子
#Losing Battle
#演示更改后循环
print("Your lone hero is surrounded by a massive army of trolls. ")
print("Their decaying green bodies stretch out,melting into the horizon.")
print("Your hero unsheathes his sword for the last fight of his life.\n")
health = 10
trolls = 0
damage = 3
while health > 0:
trolls += 1
health -= damage
print("Your hero swings and defeats an evil troll,"\
"but takes",damage,"damage points.\n")
print("Your hero fought valiantly and defeated",trolls,"trolls.")
print("But alas,your hero is no more.")
- 第3章 python实战例子 第一节
- 第2章 python实战例子
- Metasploit 实战第2章 第一节
- Python实战_0_第一周_第一节练习项目:动手做自己的网页
- 第3章Android Dalvik虚拟机 第一节
- 第3章第一节3。1-3。3顺序程序设计
- 安全参透之旅第3章 Metasploit工具 第一节
- python自动化运维 第1章系统基础信息详解 第一节
- python第一节
- Python 第一节
- python 第一节
- 第1章 python 打印输出实战
- 第5章 Python连接器的例子
- 第4章 Android dex文件格式 第一节
- brophp实战第一节
- python数据挖掘入门与实战——学习笔记(第3、4章)
- Python数据分析|第2章 引言(例子)
- Python学习,第一节
- Android样式之Selector
- 分布式日志分析系统构建实战(四)——Storm
- 传感器初涉之光照传感器的使用
- poj2184 Cow Exhibition(01背包+变化)
- ':app:transformClassesWithDexForDebug'. > com.android.build.transform.api.TransformException: when i
- 第3章 python实战例子 第一节
- Android样式之layer-list
- 第一篇
- JSP 中的 Error Page
- Android样式之drawable
- awk
- UIActivityIndicatorView 的使用
- vagrant学习笔记 - 基本命令的使用
- Android样式之Style