How to stop user input once a match is made

Viewed 113

So, I am finally getting down on this piece of work, but I can't seem to stop it from asking for user input. Basically, what I want the code to do, is get a number from the user, and if it has a match, then go ahead and print out each "roll # (number) was (number)." If the first input does not have a match, then it may ask again (up to three times till a match is made). If a match is made, the user wins, if not, the computer wins. I think range(3) might be my issue but I am just not sure, as I did try removing it.

dashes = 65
dashes_count = 65 * "-"

print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)

import random

die = 0
roll = 0

def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll

for die in range(3):
    die1 = int(input(f'Choose a number between 2 and 12: '))
    die2 = int(input(f'Choose a number between 2 and 12: '))
    die3 = int(input(f'Choose a number between 2 and 12: '))
    roll1 = dice_roll()
    roll2 = dice_roll()
    roll3 = dice_roll()
    if die1 == roll1:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    if die2 == roll2:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    if die3 == roll3:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    else:
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Lose! - Thanks for playing!')

I am looking for an output that works like this:

Choose a number between 2 and 12: 3 # user input
roll # 1 was 5
roll # 2 was 3 # the matching number
roll # 3 was 11
You Win! - Thanks for playing!
# where the user input matches 1 or all rolls and prints all roll results.
# otherwise 
Choose a number between 2 and 12: g # not valid / not a match
Choose a number between 2 and 12: 5 # the second try
roll # 1 was 7
roll # 2 was 12
roll # 3 was 5 # the matching number
You Win! - Thanks for playing
# " Choose a number between 2 and 12: " is limited to 3 tries only
# if all three tries receive in valid / unmatches numbers, print "You lose!"
3 Answers

This should be your code:

In [1899]: die = 0
      ...: roll = 0
      ...: 
      ...: def dice_roll():
      ...:     dieroll = random.randint(1, 6) + random.randint(1, 6)
      ...:     return dieroll
      ...: 
      ...: for die in range(3):
      ...:     die1 = int(input(f'Choose a number between 2 and 12: '))
      ...:     roll1 = dice_roll()
      ...:     if die1 == roll1:
      ...:         print(f'Roll # 1 was {roll1}')
      ...:         print(f'You Win! - Thanks for playing!')
      ...:     else:
      ...:         print(f'Roll # 1 was {roll1}')
      ...:         print(f'You Lose! - Thanks for playing!')
      ...: 
Choose a number between 2 and 12: 3
Roll # 1 was 6
You Lose! - Thanks for playing!
Choose a number between 2 and 12: 4
Roll # 1 was 5
You Lose! - Thanks for playing!
Choose a number between 2 and 12: 5
Roll # 1 was 9
You Lose! - Thanks for playing!

I'm not quite sure what behaviour you're looking for. Maybe thats it:

import random
dashes = 65
dashes_count = 65 * "-"

print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)


die = 0
roll = 0

def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll

for roll_number in range(3): # Do it 3 times. (3 tries for the user)
    die = int(input(f'Choose a number between 2 and 12: '))
    roll = dice_roll()
    print(f'Roll # {roll_number} was {roll}') # Tell the user what was rolled
    if die == roll:                           # Check if the user wins
      print(f'You win!')
      exit()                                  # Exit the game if he won
    else:
      print('Thats not it! Try again!')       # Tell him that he lost

print('You lost, thanks for playing!')        # If he arrives here, he has 
                                              # lost all 3 times and the programm is finished executing

Output:

You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 5
Roll # 0 was 11
Thats not it! Try again!
Choose a number between 2 and 12: 6
Roll # 1 was 8
Thats not it! Try again!
Choose a number between 2 and 12: 3
Roll # 2 was 8
Thats not it! Try again!
You lost, thanks for playing!
You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 5
Roll # 0 was 9
Thats not it! Try again!
Choose a number between 2 and 12: 5
Roll # 1 was 5
You win!

notice how we don't have to save 3 different rolls, and instead can just use one for every round and overwrite it, since the previous roll doesn't matter for the current round we're playing.

I believe it is easier to solve this problem with one more for cycle:

dashes = 65
dashes_count = 65 * "-"

print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)

import random


def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll


def main():
    won = False
    for i in range(3):
        if (won):
            print(f'You Win! - Thanks for playing!')
            break
        randRol = dice_roll()
        die = int(input(f'Choose a number between 2 and 12: '))
        for j in range(3):
            randRol = dice_roll()
            print(f'The ', j+1, ' roll was ', randRol)
            if(die == randRol):
                
                won = True
                break
    if (not won):
        print(f'You Lose! - Thanks for playing!')


main()
            
                        
        
        
                    
    

If we use "break" it will escape from only one of the for cycles so I used a bool variable to solve the problem. The program stops when the "won" becomes True. I hope this helped.

Output:

You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 7
The  1  roll was  3
The  2  roll was  6
The  3  roll was  6
Choose a number between 2 and 12: 7
The  1  roll was  9
The  2  roll was  6
The  3  roll was  6
Choose a number between 2 and 12: 7
The  1  roll was  6
The  2  roll was  12
The  3  roll was  10
You Lose! - Thanks for playing!
>>> 
Related