gpt4 book ai didi

python - Python 3.6 中的循环问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:20 27 4
gpt4 key购买 nike

我将在此处添加一些图片,以便更容易可视化。我正在制作一个项目,一种矿物目录等。尽管我已经设法将所有内容都写下来,但出于某种原因,当它询问您是否想查找另一个项目时,它并没有这样做。不回环吗?我什至不确定完整的问题是什么,我只有一两个月的经验。

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 。但事实并非如此。

最佳答案

通过这样做:

choiceMore = input
crystal = input

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

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

使用条件为“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:为了使选择可用,必须在循环部分之前声明字典,因为 Python 是被解释的。

关于python - Python 3.6 中的循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com