gpt4 book ai didi

python - 基于 for 循环的数字列表并查看用户输入是否在列表中?

转载 作者:行者123 更新时间:2023-11-30 22:42:29 25 4
gpt4 key购买 nike

好吧,我正在尝试做两件事,让我快速发布我的代码:

def main():

animals = ["Monkey", "Parrot", "Dog", "Koala", "Giraffe"]
favAnimal = input("What is your favorite animal?")

print("What is your favorite animal?")
if(favAnimal == animals):
print("The " + favAnimal + " is both of our favorite's!")
else:
print("We don't like any of the same animals. D;")

number= 1
for n in range(0, 5):
print(number + "." + animals[n])
number=number+1

main()

因此,在 if 语句中,我试图弄清楚如何检查用户输入是否与动物列表中的任何项目相同,以及是否打印该语句。

我想做的另一件事是在 for 循环中打印出带有升序数字的动物列表,如下所示:

  1. 猴子
  2. 鹦鹉3...等等

我尝试了不同的方法,但是 for 循环没有任何效果,它一直说我需要将 int 或 float 添加到第 18 行(打印数字行),但当我这样做时,它似乎不起作用。

谁能帮我解决这个问题吗?

最佳答案

您可以使用此修复代码(使用 Python 2.7.x):

def main():

animals = ["Monkey", "Parrot", "Dog", "Koala", "Giraffe"]
favAnimal = raw_input("What is your favorite animal?")

print("What is your favorite animal?")
if favAnimal in animals:
print("The " + favAnimal + " is both of our favorite's!")
else:
print("We don't like any of the same animals. D;")

for n in range(0, 5):
print(str(n+1) + ". " + animals[n])

main()

关于python - 基于 for 循环的数字列表并查看用户输入是否在列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120640/

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