【问题标题】:How to print variable on multiple lines and call on a single piece of code many times in Python?如何在多行上打印变量并在 Python 中多次调用单个代码?
【发布时间】:2013-10-11 05:57:53
【问题描述】:

1.我正在使用break 跳出循环,但我不知道如何让程序继续运行,除非发生这种情况。只输入while: 是无效的(或者程序告诉我),我希望即使用户输入空字符串,游戏也能继续运行。

2.有没有一种方法可以不必每次我需要时都重新输入一些代码?我有一堆响应要程序吐出,我将不得不多次使用:

if action[0]=='go':
    print("You're supposed to go to David!")
elif action[0]=='look':
    print("You can't see that")
elif action[0]=='take':
    print("You don't see the point in taking that.")
else:
    print("I don't recognise that command")

其中action 是来自玩家输入的列表。还是我每次都必须重新输入?

我不知道如何定义一个执行上述操作的函数,我什至不确定这是我应该做的。

3.我使用的一些故事描述很长,我不希望玩家不得不横向滚动太多。但我只想将它们定义为变量以节省一些打字时间。有没有解决的办法。还是我只需要每次都用 print(“““a string here”””)

4.如果字符串以“look”开头并且包含“floor”或“mess”或“rubbish”,我希望它打印某个输出。这是我目前拥有的:

if action[0]=='look':
    if 'floor' in action or 'rubbish' in action or 'trash' or 'mess' in action:
        print('onec')
    elif 'screen' in action or 'computer' in action or 'monitor' in action:
        print('oned')
    elif 'around' in action or 'room' in action or 'apartment' in action:
        print('onee')
    elif 'david' in action or 'tyler' in action or 'boy' in action or 'brat' in action or 'youth' in action:
        print('onef')
        break
    else:
        print("You can't see that")

它为任何以'look' 开头的输入打印'onec'

【问题讨论】:

  • #4 中的问题是... or 'trash' or 'mess' in action 等价于... or ('thrash') or ('mess' in action),并且像'trash' 这样的非空字符串总是评估为true;因此第一个条件总是满足的。您可以通过执行if any(x in action for x in ['floor', 'rubbish', 'trash', 'mess']) 之类的操作来使其更短更简洁 - 这会检查action 是否包含列表中的任何单词。

标签: python string variables loops printing


【解决方案1】:
  1. while 声明 requires a condition
  2. 您可以使用a function 反复调用相同的指令。
  3. "String literals can span multiple lines in several ways"
  4. 使用战略性放置的打印语句来显示action 的值,例如在if action[0]=='look'之后

最后,请不要在此问题中添加任何内容。而是提出一个新问题。这个网站对这类事情有一些具体的规定。

【讨论】:

    【解决方案2】:
    1. 要进行无限循环,请使用while True:

    2. 您可以使用 dict 来存储常见的操作字符串及其响应。

    【讨论】:

      【解决方案3】:
      1. 只要先注册字符串,等输入来了就改:

         command = "nothing"
         command = input("Enter command: ")
         while command:
        

        或者简单地说:

         while True:
        
      2. 是的,你自己想想吧。好吧,为什么不把它放在列表responses

      3. 如果真的很长,请将其放入文件中。需要时使用open() 阅读。更多关于File Processing 这将帮助您缩短代码,使其更易于阅读并提高效率。

      【讨论】:

      • 我现在已经掌握了窍门。谢谢你。 :)
      • 尽快完成游戏!
      【解决方案4】:
      1. while 需要一个必须评估的条件。如果您希望它永远循环,只需给它一个始终评估为 True 的条件,例如 4>3。如果您只使用while True:,这对每个人都是最好的,这是最明确的选择。
      2. 对于这种特定情况,我建议使用 dict() 及其 .get() 方法。像这样的:

        action_dict = {'go':"You're supposed to go to David!", 
                       'look':"You can't see that",
                       'take':"You don't see the point in taking that."  
                      }
        print(action_dict.get(action[0], "I don't recognise that command")
        

        会复制你现在正在做的事情。

      3. 在此处查看 cjrh 提供的链接:http://docs.python.org/3.3/tutorial/introduction.html#strings
      4. 我们的读心能力在 10 月份有所下降,除了“它不起作用”之外,我们需要更多信息来帮助您解决这个问题。

      【讨论】:

        猜你喜欢
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-10
        • 2016-06-23
        • 2021-02-12
        相关资源
        最近更新 更多