尽管满足条件,但循环未开始。键入命令时,将函数X返回Y

时间:2019-01-10 10:32:03

标签: python python-3.x

我正在尝试为我的游戏创建一个开始屏幕,该屏幕使用两个while循环一次用于菜单,一个用于实际游戏。默认情况下,我将alive设置为0,将location设置为[0,0],当您键入start时,游戏应该clear屏幕设置alive1print的一些文本,然后将location移至[0,1]。它会执行所有这些操作,但是另一个while循环不会启动,例如,当您键入east时,您会得到function east at "hex number"

#imports
import random
import sys
import os

#Define Global Variables
global health
health=100
global alive
alive=0
global wall
wall=1

#Define character
location=[0,0]
inventory=[]

#Clear screen
def clear():
    print ("\n" * 100)

#Define Directions
def north():
    nor=location[1]+1
    location[1]=nor
    return location
def east():
    est=location[0]+1
    location[0]=est
    return location
def south():
    sou=location[1]-1
    location[1]=sou
    return location
def west():
    wes=location[0]-1
    location[0]=wes
    return location

#Check player location
def checklocation():
    if location ==[1,1]:
        clear()
        print ("you are at the start", location)
    else:
        print ("you are headed away from the start", location)

#While loops
while alive==1:
    action=input ("What would you like to do? > ")
    if action=="east":
        print("east")
        east()
        checklocation()
    elif action=="west":
        print("west")
        west()
        checklocation()
    elif action=="north":
        north()
        checklocation()
    elif action=="south":
        south()
        checklocation()
    else:
        print("I didn't understand")

while location==[0,0]:
    action=input ("Welcome to the game type start to play: ")
    if action=="start":
        clear()
        print("You awaken on a forest path")
        print("You are facing east and looking down the path at a house")
        print("There are trees to your north and south and a cliff behind you")
        alive+=1
        north()
    else:
        print("I didn't understand")

1 个答案:

答案 0 :(得分:0)

此问题是,您的循环不正常。跳过while alive==1循环,因为该条件最初是假的。将其设置为true不会使您回到这一点。但是,如果您只是移动它们,它将按您的预期执行。

#imports
import random
import sys
import os

#Define Global Variables
global health
health=100
global alive
alive=0
global wall
wall=1

#Define character
location=[0,0]
inventory=[]

#Clear screen
def clear():
    print ("\n" * 100)

#Define Directions
def north():
    nor=location[1]+1
    location[1]=nor
    return location
def east():
    est=location[0]+1
    location[0]=est
    return location
def south():
    sou=location[1]-1
    location[1]=sou
    return location
def west():
    wes=location[0]-1
    location[0]=wes
    return location

#Check player location
def checklocation():
    if location ==[1,1]:
        clear()
        print ("you are at the start", location)
    else:
        print ("you are headed away from the start", location)

while location==[0,0]:
    action=input ("Welcome to the game type start to play: ")
    if action=="start":
        clear()
        print("You awaken on a forest path")
        print("You are facing east and looking down the path at a house")
        print("There are trees to your north and south and a cliff behind you")
        alive+=1
        north()
    else:
        print("I didn't understand")

while alive==1:
    action=input ("What would you like to do? > ")
    if action=="east":
        print("east")
        east()
        checklocation()
    elif action=="west":
        print("west")
        west()
        checklocation()
    elif action=="north":
        north()
        checklocation()
    elif action=="south":
        south()
        checklocation()
    else:
        print("I didn't understand")