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,728評論 0 89
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,523評論 0 17
  • 1 順序語句 語句:使用分號分隔的代碼稱作為一個語句。 注意:沒有寫任何代碼只是一個分號的時候,也是一條語句,...
    哈哈哎呦喂閱讀 408評論 0 0
  • 循環簡介 循環可以用于讓一個程序重復地執行語句。 循環是用來控制語句塊重復執行的一種結構。 循環的概念是程序設計的...
    Vinfai閱讀 1,192評論 0 0
  • 燈光幽幽 外面的昆蟲的叫聲 不平靜,不枯寂 今晚有沒有星星啊 多愿那天空 容下滿瓶輝光
    泛夜孤舟閱讀 160評論 0 0