【发布时间】:2016-04-21 17:39:45
【问题描述】:
我是一个相当新的程序员,并且已经使用 Python 3 工作了几个星期。我尝试制作一个小魔术 8 球程序,您可以在其中得到一个问题的答案,并询问您是否想再玩一次。但是,无论我输入什么,它都不会退出并继续循环。我不确定我做错了什么。任何帮助是极大的赞赏!
#Magic 8 Ball V2
import random
import time
class Magic8ball:
def __init__(self, color):
self.color = color
def getanswer(self):
responselist = ['The future looks bright!', 'Not too good...', 'Its a fact!',
'The future seems cloudy', 'Ask again later', 'Doesnt look too good for you',
'How would i know?', 'Maybe another time']
cho = random.randint(0, 7)
print ('Getting answer...')
time.sleep(2)
print (responselist[cho])
purple = Magic8ball('Purple')
blue = Magic8ball('Blue')
black = Magic8ball('Black')
while True:
print ('Welcome to the magic 8 ball sim part 2')
input('Ask your question:')
black.getanswer()
print ('Would you like to play again?')
choice = ' '
choice = input()
if choice != 'y' or choice != 'yes':
break
【问题讨论】:
-
你的布尔逻辑错了……
-
如果你从不放弃,那么其他事情正在发生。正如所写,它应该始终退出。
标签: python loops user-input