0

So I'm having some kind of problem with this code that should simulate a dice rolling simulator but i cant get out of my while loop again whatever i try.

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()
vallentin
  • 23,478
  • 6
  • 59
  • 81
  • 1
    `' yes'` will always be true – Sayse Jan 06 '19 at 21:01
  • "while True" set you in an infinite loop. – Daedalus Jan 06 '19 at 21:01
  • @HectorIX - Thats irrelevant, the `quit()` will close the application – Sayse Jan 06 '19 at 21:02
  • You are right. However this is not the best practice. It will be much better if you define a variable, set it to True and when choose 'No', then assign the False. – Daedalus Jan 06 '19 at 21:04
  • @HectorIX - I disagree, theres nothing wrong with it here, in fact in an environment that requires extremely strict memory management, the extra variable may even be a hinderance – Sayse Jan 06 '19 at 21:06

1 Answers1

0

You need to check the variable against the value everytime. In the line

if rollAgain == 'yes' or ' yes':

Python evaluates ' yes' as true everytime. You need to compare it to rollAgain aswell.

Here's the fixed code:

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()
vallentin
  • 23,478
  • 6
  • 59
  • 81
AndrejH
  • 2,028
  • 1
  • 11
  • 23