while循环只运行一次
While loop only runs one time
正如标题所说,我的 while 循环只会 运行 一次。我第一次 运行 代码时,它要求输入,我可以回答历史和地理类别中的所有问题。然后我输入 exit 并再次尝试 运行 代码,它会一直问我想要什么主题。如果我输入exit它仍然会停止,但我无法做到运行所以我可以再次回答我的问题
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
import History
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
import Geo
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break;
else:
print("Sorry, please enter a valid topic")
continue
无法再次导入 .py 文件。而是调用那些 .py 文件的函数。例如。如果 history.py 有一个名为 def questions()
的函数
并且 geo 有一个同名的函数,你可以像这样修改你的代码
import History
import Geo
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
History.questions()
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
Geo.questions()
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break
else:
print("Sorry, please enter a valid topic")
continue
正如@tdelaney 在评论中建议的那样,无法再次导入模块。
您可以做的是在 History
和 Geo
中编写一个函数,并在需要时调用它们。
试试这个:
对于History.py
def askQuestion():
print('the Question')
在Geo.py
中做同样的事情
对于main.py
import History
import Geo
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
History.askQuestion()
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
Geo.askQuestion()
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break;
else:
print("Sorry, please enter a valid topic")
continue
输入退出后无法再次运行此代码。因为 break 关键字终止了在 exit 下定义的循环。
但是你可以使用一些技巧来继续退出代码。
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
import History
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
import Geo
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
play_again = input("Are want to play again(y/N)?")
if play_again.lower()=='y':
continue
else:
break
else:
print("Sorry, please enter a valid topic")
continue
正如标题所说,我的 while 循环只会 运行 一次。我第一次 运行 代码时,它要求输入,我可以回答历史和地理类别中的所有问题。然后我输入 exit 并再次尝试 运行 代码,它会一直问我想要什么主题。如果我输入exit它仍然会停止,但我无法做到运行所以我可以再次回答我的问题
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
import History
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
import Geo
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break;
else:
print("Sorry, please enter a valid topic")
continue
无法再次导入 .py 文件。而是调用那些 .py 文件的函数。例如。如果 history.py 有一个名为 def questions()
的函数
并且 geo 有一个同名的函数,你可以像这样修改你的代码
import History
import Geo
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
History.questions()
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
Geo.questions()
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break
else:
print("Sorry, please enter a valid topic")
continue
正如@tdelaney 在评论中建议的那样,无法再次导入模块。
您可以做的是在 History
和 Geo
中编写一个函数,并在需要时调用它们。
试试这个:
对于History.py
def askQuestion():
print('the Question')
在Geo.py
对于main.py
import History
import Geo
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
History.askQuestion()
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
Geo.askQuestion()
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
break;
else:
print("Sorry, please enter a valid topic")
continue
输入退出后无法再次运行此代码。因为 break 关键字终止了在 exit 下定义的循环。
但是你可以使用一些技巧来继续退出代码。
while True:
choose = input("What topic would you like?")
if choose.lower() == "history":
import History
continue
elif choose.lower() == "geo" or choose.lower() == "geography":
import Geo
continue
elif choose.lower() == "i'm done" or choose.lower() == "exit":
print("Thanks for playing!")
play_again = input("Are want to play again(y/N)?")
if play_again.lower()=='y':
continue
else:
break
else:
print("Sorry, please enter a valid topic")
continue