Python3.6中的循环问题

Python3.6中的循环问题,python,for-loop,directory,Python,For Loop,Directory,我将在这里添加一些图片,这样更易于可视化。我正在做一个项目,一个矿物目录等。虽然我已经设法把所有的东西都写下来了,但出于某种原因,当它问你是否想查找另一个目录时,它不会返回?我甚至不知道到底是什么问题,我只有一两个月的经验 我在Python3.6的整个代码中都使用IF和ELIF语句,但就我个人而言,我不知道如何保持一个恒定的(是/否)流,所以在每个晶体或矿物之后,你可以要求阅读另一个晶体或矿物 代码如下: 还有更多的代码,但这是一个循环,应该允许您键入另一个晶体。但事实并非如此。通过执行以

我将在这里添加一些图片,这样更易于可视化。我正在做一个项目,一个矿物目录等。虽然我已经设法把所有的东西都写下来了,但出于某种原因,当它问你是否想查找另一个目录时,它不会返回?我甚至不知道到底是什么问题,我只有一两个月的经验

我在Python3.6的整个代码中都使用IF和ELIF语句,但就我个人而言,我不知道如何保持一个恒定的(是/否)流,所以在每个晶体或矿物之后,你可以要求阅读另一个晶体或矿物

代码如下:



还有更多的代码,但这是一个循环,应该允许您键入另一个晶体。但事实并非如此。

通过执行以下操作:

choiceMore = input
crystal = input
您正在将内置函数指定给变量。但是很难说为什么,这会导致覆盖前一行中输入调用返回的值

(crystal变量不再引用从stdin接收的字符串,而是引用内置函数)

使用带有“True”条件的while循环,该循环将无限期地继续,直到使用“break”语句中断为止

    while True:
        print("Would you like to find out about another crystal?")
        choice = input("Yes or No?").lower()
        if choice == "yes":
            crystal = input("Please enter a crystal name").lower()
            # Do whatever you do with your crystals.
        else:
            print("Thanks for using Space Statue's Crystal Directory!")
            time.sleep(1)
            print("Please come back soon to fufil your crystal needs!")
            break
另一个选项是递归调用相同的函数:

    def moreCrystals():
        print("Would you like to find out about another crystal?")
        choice = input("Yes or No?").lower()
        if choice == "yes":
            crystal = input("Please enter a crystal name").lower()
            # Do whatever you do with your crystals.
            # THE RECURSIVE CALL
            moreCrystals()
        else:
            print("Thanks for using Space Statue's Crystal Directory!")
            time.sleep(1)
            print("Please come back soon to fufil your crystal needs!")

    moreCrystals()
我假设这是某种练习,否则应该将这些文本保存在数据库中。每个包含字符串的变量都会占用内存

无论如何,您可以使用字典(key:value)来存储您的选择:

choices = {"opal": """Opal. Also known as Opalite.

              ----------------------------
              keywords - ORIGINALITY // CREATIVITY // CONFIDENCE //
              COMFORTABILITY // 
              ----------------------------

              Properties: Most commonly a blue, translusent stone. Can have
              coloured flashes of all shades. Looks like a dragon egg. It is
              the birth stone of those who fall under the Star Sign Libra.

              Meaning: A stone that inspires originality and boosts creativity.
              The energy of the stone encourages confidence and being comfortable
              within yourself. Being a highly absorbent energy stone, Opal will
              take your emotions, thoughts and feelings, magnify them and send
              them back to you, so use this stone in moments of positivity and
              confidence.

              Origins: Australia, Mexico, Brazil, Indonesia,
              Czech Republic, Ethiopia and USA. 

              Rarity level: common"""
    "tourmaline": """ Tourmaline.

              ----------------------------
              keywords - UNDERSTANDING // INSPIRATION // COMPASSION //
              TOLERANCE // PROSPERITY // BALANCING MALE-FEMALE ENERGY //
              ENHANCES ENERGY //
              ----------------------------

              Properties: It is made from a crystal silicate mineral. It is
              most commonly black, but can range from brown, violet, green, pink,
              or in a dual-coloured pink and green.

              Meaning: Tourmaline aids in understanding oneself and others.
              It promotes self-confidence and diminishes fear. Tourmaline attracts
              inspiration, compassion, tolerance and prosperity. It balances the
              right-left sides of the brain. Helps treat paranoia, overcomes
              dyslexia and improves hand-eye coordination. Tourmaline releases tension,
              making it helpful for spinal adjustments. It balances male-female energy
              within the body. Enhances energy and removes blockages.


              Origins: Afghanistan, Pakistan, Russia, Burma, Sri Lanka and the
              United States.

              Rarity level: Between common and uncommon. """}
并可通过以下键访问:

crystal = input("Please enter a crystal name").lower()
# Do whatever you do with your crystals.
print(choices[crystal])

PS:要使选项可用,必须在循环部分之前声明dict,正如Python所解释的那样。

您似乎有第二个
crystal=input
lines,您正在将内置的
input()
函数分配给
choiceMore
crystal
。请尽量减少代码片段。长字符串可能会导致问题或解决方案,因此您应该缩短它们。它阻止人们研究你的问题。@gal242我对编码还是相当陌生,所以我不确定你的意思是什么?@gal242哦,我明白了。。。我的道歉,以及编码我还是相当新的这个网站,以及,我会记住,在未来!谢谢你,我来看看!我对Python还是相当陌生,所以我仍然把函数搞混了——我会看看这是怎么回事!我更新了代码,这样你就可以看到更多我想做的事情
crystal = input("Please enter a crystal name").lower()
# Do whatever you do with your crystals.
print(choices[crystal])