簡體   English   中英

Python 3.6 中的循環問題

[英]Loop problems in Python 3.6

我將在這里添加一些圖片,以便更容易地進行可視化。 我正在做一個項目,一種礦物目錄等。雖然我已經設法把所有的東西都寫好了,但出於某種原因,當它詢問你是否要查找另一個時,它沒有t循環? 我什至不確定完整的問題是什么,我只有一兩個月的經驗。

這是我遇到的問題的視覺效果。 每當我嘗試查找另一種晶體或礦物時,我都會遇到這個問題和模式。

我在 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()

代碼還有更多內容,但這是應該允許您輸入另一個晶體的循環。但它沒有。

通過做:

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. """}

並且可以通過key訪問:

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

PS:為了使選擇可用,必須在循環部分之前聲明 dict,因為 Python 是被解釋的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM