python自学项目day2.定时器time和随机数模块random以及while循环

利用定时器time添加2秒间歇停顿

利用随机数模块random添加随机选取

import random
import time

print("You have reached the opening of a cave you decide to arm yourself with an")

time.sleep(2)

quest_item = input("think of an object\n")

print("you look in you backpack for",quest_item)

time.sleep(2)

print("you could not find",quest_item)
print("you select any item that comes to hand from the backpack instead\n")

time.sleep(2)

inventory = ["Torch","Pencil","Rubber band","Capapult","Rope"]

print(random.choice(inventory))

条件判断

import time

print("You are standing on a path at the edge of a jungle.There is a cave to your left and a beach to your right")

time.sleep(0.5)

direction1 = input("Do you want to go left or right?")

if direction1 == "left":
  print("You walk to the cave and notice there is an openning")

elif direction1 == "right":
  print("you walk to the beach but remember you do no have any swimwear.")

else:
  print("you should think for a while")

添加while循环(只要...条件存在,就一直做...)

因为在while后面的表达式永远成立,所以print一直会进行下去
在终端中按ctrl+c停止运行

一直循环无中止,无视大小写

#loop until we get a recognised response
while True:
  direction1 = input("Do you want to go left or right?")
  direction1 = direction1.lower() # transform input into lower format
  if direction1 == "left":
    print("you walk to the cave and notice there is an opening.")
  elif direction1 == "right":
    print("you walk to the beach but remember you do not have any swimwear.")
  else:
    print("You think for a while")
Paste_Image.png

改进添加break并引入hp变量作为生命值

import time

#Define variables
hp = 30

print("you are standing on a path at the edge of a jungle. There is a cave to your")
time.sleep(1)

#loop until we get a recognised response
while True:
  direction1 = input("Do you want to go left or right?")
  direction1 = direction1.lower() # transform input into lower format
  if direction1 == "left":
    print("you walk to the cave and notice there is an opening.")
    print("A small snake bites you,and you lose 20 health points")
    hp -= 30
    if hp <= 0:
       print("You are dead.I am sorry.")
       break
  elif direction1 == "right":
    print("you walk to the beach but remember you do not have any swimwear.")
    print("the cool water revitalizes you. you have never felt more alive, gain 70 HP")
    hp += 70
    print("You've got win")
    break
  else:
    print("You think for a while")
    hp -= 10
    if hp <= 0:
       print("You are dead.I am sorry.")
       break
  print("you now have",hp,"health points")
# check health points after the player has made a move
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 回溯算法 回溯法:也称为试探法,它并不考虑问题规模的大小,而是从问题的最明显的最小规模开始逐步求解出可能的答案,并...
    fredal阅读 13,763评论 0 89
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,571评论 0 17
  • 1 顺序语句 语句:使用分号分隔的代码称作为一个语句。 注意:没有写任何代码只是一个分号的时候,也是一条语句,...
    哈哈哎呦喂阅读 413评论 0 0
  • 循环简介 循环可以用于让一个程序重复地执行语句。 循环是用来控制语句块重复执行的一种结构。 循环的概念是程序设计的...
    Vinfai阅读 1,202评论 0 0
  • 灯光幽幽 外面的昆虫的叫声 不平静,不枯寂 今晚有没有星星啊 多愿那天空 容下满瓶辉光
    泛夜孤舟阅读 160评论 0 0