因此我在这段代码中存在某种问题,应该模拟骰子滚动模拟器,但是无论尝试如何,我都无法再次退出while循环。
import random
print('-------------------------------------------------------')
print(' WELCOME TO THE DICE ROLLING SIMULATOR')
print('-------------------------------------------------------')
while True:
randomNumber = str(random.randint(1, 6))
rollAgain = input('would you like to roll the dice? yes or no?')
if rollAgain == 'yes' or ' yes':
print('Rolling the dice')
print('the dice landed on the number: ' + randomNumber)
elif rollAgain == 'no' or ' no':
quit()
答案 0 :(得分:0)
您需要每次都对照该值检查变量。在行中
if rollAgain == 'yes' or ' yes':
Python每次都将' yes'
识别为true
。您还需要将其与rollAgain
进行比较。
这是固定代码:
import random
print('-------------------------------------------------------')
print(' WELCOME TO THE DICE ROLLING SIMULATOR')
print('-------------------------------------------------------')
while True:
randomNumber = str(random.randint(1, 6))
rollAgain = input('would you like to roll the dice? yes or no?')
if rollAgain == 'yes' or rollAgain == ' yes':
print('Rolling the dice')
print('the dice landed on the number: ' + randomNumber)
elif rollAgain == 'no' or rollAgain == ' no':
quit()