利用定時(shí)器time添加2秒間歇停頓
利用隨機(jī)數(shù)模塊random添加隨機(jī)選取
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循環(huán)(只要...條件存在枣申,就一直做...)
因?yàn)樵趙hile后面的表達(dá)式永遠(yuǎn)成立权薯,所以print一直會(huì)進(jìn)行下去
在終端中按ctrl+c停止運(yùn)行
一直循環(huán)無(wú)中止倦沧,無(wú)視大小寫
#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
改進(jìn)添加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