python 3滚动骰子模拟器的问题

时间:2019-01-06 20:58:21

标签: python python-3.x

因此我在这段代码中存在某种问题,应该模拟骰子滚动模拟器,但是无论尝试如何,我都无法再次退出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()

1 个答案:

答案 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()