第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.")

0 0
原创粉丝点击