到目前为止,这就是我所得到的。
import random
answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")
answer4=("There's different ways to do this.")
answer5=("Definitely not")
print("Welcome to the Magic 8 Ball game-use it to answer your questions...")
questio = input("Ask me for any advice and I'll help you out. Type in your question and then press Enter for an answer")
print("Shaking... \n" * 4)
choice=random.randint(1,5)
if choice == 1:
answer=answer1
elif choice == 2:
answer=answer2
elif choice == 3:
answer=answer3
elif choice == 4:
answer=answer4
elif choice == 5:
answer=answer5
print(answer)
restart = input("Would you like to try again?")
if restart == "yes" or "y":
现在我需要为此添加一个循环,以便在完成后显示“您想再试一次吗?”。输入 yes 后,程序再次从顶部开始。
尝试在代码中添加这样的循环:
restart = ''
while restart.upper() not in ['NO', 'N', 'EXIT']:
# ...
# Your code here
# ...
restart = input("Would you like to try again?")
这本质上是将 restart 初始化为空字符串。然后循环只会开始 whilerestart != 'NO'
。在循环结束时,我们从用户那里获得一个新值restart
所以如果他们说'no'
or 'n'
循环不会再次开始。
还有很多其他方法可以做到这一点,例如相反,您可以检查用户是否输入了肯定答案('yes'
or 'y'
)或放一个break
如果他们说'no'
.
希望这能让您开始。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)