【问题标题】:Loop problems in Python 3.6Python 3.6 中的循环问题
【发布时间】:2018-08-15 13:33:46
【问题描述】:

我将在此处添加几张图片,以便更易于可视化。我正在制作一个项目,一种矿物目录等。虽然我已经设法把所有东西都写好了,但出于某种原因,当它询问你是否想查找另一个时,它没有t 环回?我什至不确定完整的问题是什么,我只有一两个月的经验。

This is a visual to the problem I'm having. Whenever I try to look up another crystal or mineral, I keep getting this problem and pattern.

我在 Python 3.6 的整个代码中都使用了 IF 和 ELIF 语句,但是对于我的一生,我只是不知道如何在每个晶体或矿物之后保持恒定(是/否)流,你可以要求阅读另一个。

这里是代码:


   import time

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



print("""         Welcome to Space Statue's Crystal Directory!
                  Simply type in a crystal name or type,
                  and we will do the rest! This directory will
                  tell you the following about the chosen crystal:

                  1. It's Properties and Meaning.

                  2. A brief description of the mineral.

                  3. Where it orginates/comes from in the world.

                  4. The mineral's rarity level.

                  I hope that this directory helps fufil your crystal
                  needs!              """)




crystal = input("Please enter a crystal name").lower()
if crystal == "opal":
    print(""" 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 """)
    moreCrystals()


elif crystal == "tourmaline":
    print(""" 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. """)
    moreCrystals()

代码还有更多内容,但这是允许您输入另一个水晶的循环。但它没有。

【问题讨论】:

  • 你似乎还有第二条crystal = input
  • 您将内置的input() 函数分配给choiceMorecrystal
  • 请尽量减少您的代码 sn-ps。有助于问题或解决方案的长字符串,因此您应该缩短它们。它会阻止人们调查您的问题。
  • @gal242 我对编码还很陌生,所以我不确定你的意思?
  • @gal242 哦,我明白了...我的道歉,以及编码我对这个网站还是相当新的,我会记住的!

标签: python for-loop directory


【解决方案1】:

通过做:

choiceMore = input
crystal = input

您正在将内置函数分配给变量。但很难说出原因,这会导致覆盖上一行输入调用返回的值。

(“水晶”变量不再引用从标准输入接收到的字符串,而是引用了内置函数)

使用带有“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()

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

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

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 是被解释的。

【讨论】:

  • 谢谢我去看看!我对 Python 还很陌生,所以我仍然把函数弄混了——我会看看这是怎么回事!
  • 我更新了代码,以便您可以看到更多我正在尝试做的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 2016-11-07
  • 1970-01-01
相关资源
最近更新 更多