Mickie Schiazza
Mickie Schiazza

Reputation: 41

Loop problems in Python 3.6

I'm going to add a few pictures here so it's easier to visualise. I'm in the process of making a project, a sort of directory for minerals etc. Whereas I've managed to get everything written up, for some reason when it asks if you'd like to look up another one, it doesn't loop back? I'm not even sure what the full problem is, I only have a month or two's experience with it.

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.

I'm using IF and ELIF statements throughout the code in Python 3.6 but still, for the life of me I just don't know how to keep a constant (Yes/No) stream going so after every crystal or mineral, you can ask to read about another.

And here is the code:


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

There is more to the code as well but this is the loop which should allow you to type another crystal in. But it doesn't.

Upvotes: 1

Views: 102

Answers (1)

Aleksei Maide
Aleksei Maide

Reputation: 1855

By doing:

choiceMore = input
crystal = input

You are assigning the in-built function to the variable. Hard to tell why though, which result in overriding the value returned by the input invocation on the previous line.

(The "crystal" variable doesn't reference the string received from the stdin anymore, but references the in-built function)

Use a while loop with "True" condition, which will continue indefinitely, until you break out of it using "break" statement.

    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

Another option is do call the same function recursively:

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

I assume this is some sort of practice exercise, otherwise you should keep those text in a database. Every variable containing a string takes up memory.

Anyway, you could use a dictionary (key: value) to store your choices:

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

And can be accessed by key:

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

PS: To make the choices available the dict has to be declared before the loop part, as Python is interpreted.

Upvotes: 1

Related Questions