我正在尝试弄清楚如何在Python中使用循环,我需要一点帮助。我写的代码只是我在玩的东西。这不是一个任务或者类似的东西。
我试图弄清楚的是如何循环程序,以便它要求用户输入一些东西来开始问题。这就是我到目前为止所知道的:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
for question in is_it_a_monkey:
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
continue
else:
print(monkey_yes)
break
def main():
while True:
if again not in {"y","n"}:
print("Please enter valid input")
elif again == "n":
return "Good bye!"
elif again == "y":
return monkeys()
monkeys()我正在尝试让main()来做大部分的工作,因为这是我的老师在我们的作业中想要的。main()下的所有东西都是我复制的东西,看它是否可以工作,但它只返回以下内容:
Type in Gorilla, Chimp or Macaque and make sure they're capitalized: Gorilla
This is not a monkey!
This is not a monkey!
This is not a monkey!
This is not a monkey!它比仅仅4行要长得多,但这不是我要找的。
发布于 2020-10-16 23:38:19
我发现这里有几个问题...
首先,你要连接两个字符串...
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes当您进入for循环时,解释器将查看该连接字符串的每个字符。这就是为什么你输出38行“这不是一只猴子!”对于您正在尝试做的事情,您不需要For循环。试着这样做:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
else:
print(monkey_yes)我看到的下一个问题是根本不调用main函数。调用main()而不是在代码底部调用moneys()。
下一个问题是使用"while True:“。如果要使用布尔值作为while条件,请确保在代码中加入逻辑以更改该条件。将其更改为"False“应该是退出main语句的内容,而不是return语句。您的main()最好像这样开始:
def main():
keep_going = True
while keep_going:
monkeys()请注意,您应该首先调用monkeys()函数,否则在程序启动时没有人知道要输入什么。您还需要代码询问他们是否想要继续运行程序。在您的monkey()调用之后,执行如下操作:
monkeys()
again = input("Would you like to try again? (y/n) ")下一个问题是您对return语句的使用。而不是这样做:
elif again == "n":
return "Good bye!"这样做..。
elif again == "n":
print("Good bye!")
keep_going = False最后,你有“如果又不在{"y","n"}:”。你必须给“再次”赋值,否则你会得到更多的错误。如果你使用上面的例子,它应该能满足你的需求。
继续努力,不要失去希望。你就快理解它了。
发布于 2020-10-16 23:12:04
首先,调用monkeys(),但应该改为调用main(),因为这是循环所在的位置。其次,return会导致函数暂停执行,并在调用它的地方继续执行。要继续循环,请删除main()中的return。
要更好地理解代码是如何工作的,请阅读关于调试的this article。它显示了一些提示,使您可以一步一步地了解您的代码正在做什么。
发布于 2020-10-16 23:32:52
你应该改用这段代码,正如许多人所指出的(包括上面的@ code -Apprentice ),它会按照你的意图调用函数main()。@ code -Apprentice的答案也很好地解释了为什么这样做。这段代码与上面的代码几乎完全相同,只有最后一行不同:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
for question in is_it_a_monkey:
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
continue
else:
print(monkey_yes)
break
def main():
while True:
if again not in {"y","n"}:
print("Please enter valid input")
elif again == "n":
return "Good bye!"
elif again == "y":
return monkeys()
main()我提交这个答案的原因是要指出,尽管调用了main(),您仍然会立即遇到另一个问题。您在main()中引用了一个变量again,但是从一开始就没有给again赋值(如果您运行此代码,您可能会看到这样的错误:"NameError:全局名称‘再一次’没有定义。“)。解决下一个错误超出了您的问题的范围;如果您愿意,有很多方法可以重写此代码以适应again。
https://stackoverflow.com/questions/64391810
复制相似问题