I cannot for the life of me fix my endless loop

Question:

I am making a dice roller to roll two die and give the total, once user prompts n for another roll, it should stop, The problem I am having is script rolls two dices forever without prompting choice2, pls help.

print('Dice Roller')
print()
choice1 = 'y'
choice2 = 'y'
choice1 = input('Roll the dice? (y/n): ')
while choice1 == 'y':
    import random
    possibleResults = [1, 2, 3, 4, 5, 6]
    result1 = random.choice(possibleResults)
    result2 = random.choice(possibleResults)
    total = (result1 + result2)
    print('Die 1: ', str(result1))
    print('Die 2: ', str(result2))
    print('Total: ', total)
    print()
    if (result1, result2) == 1:
        print('Snake eyes!')
        print()
    elif (result1, result2) == 6:
        print('Boxcars!')
        print()
        choice2 = print('Roll again? (y/n): ')
        if choice2 == 'y':
            continue
        else:
            break
Asked By: DokkanFred

||

Answers:

The 2 main problems are that the conditions for your ìf statements will never be true and then you are using print() where you ask for input.

You can change the condition on your if to:

if (result1, result2) == (1, 1):

or:

if result1 == 1 and result2 == 1:

And the line where you ask for choice again, you should change it to:

choice2 = input('Roll again? (y/n): ')

Just like the first time you ask for input.

After that it seems to be working for me, but you are still only asking if they should continue whenever you roll double 6. You might want to put that out of the elif so it asks after every roll. You are also using two choice variables and then using an if to exit the loop, but because you are using the first variable in the loop condition you can re-use that one and get the same effect. Something like:

print('Dice Roller')
print()
choice = 'y'
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
    import random
    possibleResults = [1, 2, 3, 4, 5, 6]
    result1 = random.choice(possibleResults)
    result2 = random.choice(possibleResults)
    total = result1 + result2
    print('Die 1: ', str(result1))
    print('Die 2: ', str(result2))
    print('Total: ', total)
    print()

    if (result1, result2) == (1, 1):
        print('Snake eyes!n')
    elif (result1, result2) == (6, 6):
        print('Boxcars!n')
    
    choice = input('Roll again? (y/n): ')
Answered By: Topo

These conditions can never be true:

if (result1, result2) == 1:
    print('Snake eyes!')
elif (result1, result2) == 6:
    print('Boxcars!')

because a tuple can’t equal a single int. Instead you probably want to do:

if (result1, result2) == (1, 1):
    print('Snake eyes!')
elif (result1, result2) == (6, 6):
    print('Boxcars!')

Or equivalently you could do something like:

if total == 2:
    print('Snake eyes!')
elif total == 12:
    print('Boxcars!')

Note also that your prompt for another roll after boxcars doesn’t actually input from the user. If you just get that input and simply assign it to the variable that your while loop’s predicate is based on (rather than having two different variables, which is unnecessarily complicated), you don’t need to explicitly continue or break the loop.

A couple of other minor notes: I’d suggest using random.randint instead of random.choice from a hardcoded list, and I’d also suggest just putting the results in a tuple to begin with rather than having to re-construct it as result1, result2 in a bunch of places.

import random

print('Dice Rollern')
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
    results = random.randint(1, 6), random.randint(1, 6)
    total = sum(results)
    for i, result in enumerate(results, 1):
        print(f'Die {i}: {result}')
    print(f'Total: {total}n')
    if total == 2:
        print('Snake eyes!n')
    elif total == 12:
        print('Boxcars!n')
        choice = input('Roll again? (y/n): ')
Answered By: Samwise
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.