我将如何设计此代码,当我进行循环时,前一个循环中的(x,y)值将存储在程序中。现在,如果我向程序添加一个循环块,则每个输入都会重置(x,y)值。
question = (input("Where do you want to go?: "));
y = 0
x = 0
if question in ["up"]:
y = y + 1
print("You've moved up one unit!")
print("Your position is now ", x, y)
if question in ["down"]:
y = y - 1
print("You've moved down one unit!")
print("Your position is now ", x, y)
if question in ["right"]:
x = x + 1
print("You've moved right one unit!")
print("Your position is now ", x, y)
if question in ["left"]:
y = y + 1
print("You've moved left one unit!")
print("Your position is now ", x, y)
else:
print("You can't go there.")
答案 0 :(得分:0)
如果你添加一个循环和x,y重置每个输入,听起来你需要将x,y移出循环:
x=0
y=0
while True:
# ask the question
# process
而不是:
while True:
# ask the question
x=0
y=0
# process
答案 1 :(得分:0)
你应该在循环之外初始化x和y
y = 0
x = 0
while True:
question = (input("Where do you want to go?: "))
...
否则每次执行到达循环顶部时都会执行这些行