繁体   English   中英

如何让python返回到一行代码?

[英]How do I get python to go back to a line of code?

我对 python 和编码相对较新,但我目前正在尝试制作一个模块化游戏,你可以根据给你的选项输入你想做的事情。 我过去使用过一个 while 循环,它涵盖了一个有多项选择的问题:

    while(cont==False):
Intro2=input("Continue; New Game; Settings; Save Files; ")
if(cont==False):

if(Intro2=="Continue"):
  print("This option is currently not available.")
elif(Intro2=="Settings"):
  print("This option is currently not available.")
elif(Intro2=="Save Files"):
  print("This option is currently not available.")
elif(Intro2=="New Game"):
  N_G=input("New game selected. Continuing will erase all previous save data. Are you sure you want to continue? 'yes' 'no': ")
else:
  print("Your response is invalid. Make sure you are using the exact wording, including grammar and spelling.")
  
if(N_G=="yes"):
  print("redirecting")
  time.sleep(1)
  print(".")
  time.sleep(1)
  print(".")
  time.sleep(1)
  print(".")
  cont=True
elif(N_G=="no"):
  print("redirecting")
  cont=False
else:
  print("Your response is invalid. Make sure you are using the exact wording, including grammar and spelling.")
  cont=False
  break 

但我不想每次有输入时都这样做,我不确定是否有更简单的方法来做到这一点。 我试图实现的特定代码区域是:

    q1=input("you lost a bet to the local wizard and now you have to hold your end of the bargain. You lay in your room, staring at the ceiling, knowing the criticism you'll get as soon as you walk out your door; 'get up': ")
if(q1=="get up"):
  print("you get up out of your bed and look around. Your room is still basic as ever, only 
consisting of 5 things: your bed, dresser, desk, window, and door.")
else:
  print("Your response is invalid. Make sure you are using the exact wording, including 
grammar and spelling.")

从左边进入:python 函数。 您可以在此处了解有关它们的所有信息。 简而言之,它允许您在任何想要保持代码动态的地方基本上重用代码块,并消除到处重复复制/粘贴代码的需要

以下是您可以执行的操作的示例:

 def your_function_here():
   q1=input("you lost a bet to the local wizard and now you have to hold your end of the bargain. You lay in your room, staring at the ceiling, knowing the criticism you'll get as soon as you walk out your door; 'get up': ")
  if(q1=="get up"):
    print("you get up out of your bed and look around. Your room is still basic as ever, only 
consisting of 5 things: your bed, dresser, desk, window, and door.")
  else:
    print("Your response is invalid. Make sure you are using the exact wording, including 
grammar and spelling.")

 your_function_here()

在您调用 your_function_here() 的代码中的任何地方,它将运行该代码块

在 Python 中,缩进非常重要。 这在 Java 和 C 等其他语言中不是这种情况。但是要声明一行发生在 while 循环中,将所述行缩进两个空格(或 4,取决于您在 Python 中的设置。

在您的第一行中,您的 while 循环比以下语句缩进得更远,这应该会导致错误。 我会把所有缩进的东西都放在循环里。

while (cont == False):
    input2 = input("Continue; New Game; Settings; Save Files")
    if input2 == ...:
        ...
    elif input2 == ...:
        ...

print("This line will be outside of the loop")

一旦你这样做了,试着把你的大代码块分割成更小的函数,就像 Halmon 所说的那样。

保持良好的工作!

正如 Halmon 所说,这是一个了解函数的好机会。 当您发现自己一遍又一遍地做大致相同的事情(即使不是完全相同的事情)时,您应该定义一个函数来做那件事是一个很好的线索。

我看到你的代码反复做的事情是“提示输入一组选项中的一个,如果输入无效则重新提示”。 所以让我们把那个特定的东西变成一个函数:

def prompt_with_options(option_list: list) -> str:
    """Prompt for one of the options in the list until a valid option is chosen."""
    while True:
        option = input("; ".join(option_list))
        if option in option_list:
            return option
        print("Your response is invalid. Make sure you are using the exact wording.")

现在你的其余代码看起来像这样:

while True:
    option = prompt_with_options(["Continue", "New Game", "Settings", "Save Files"])
    if option is not "New Game":
        print("This option is currently not available.")
        continue

    # New Game
    print("New game selected. Continuing will erase all previous save data. Are you sure you want to continue?")
    option = prompt_with_options("yes", "no")

    print("redirecting")
    if option == "no":
        break

    # yes
    for _ in range(3):
        time.sleep(1)
        print(".")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM