第3章 python实战例子 第一节

第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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随行之旅

python国产化自动化

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值