Home > Net >  Trying to create a dice simulation game with two rolls and a returned value
Trying to create a dice simulation game with two rolls and a returned value

Time:10-17

I'm designing a function that calls a function of the randomly rolled die. If both numbers are guessed correctly, money is tripled. If non are, money is lost. If one is, the sum of die rolled is the losing target. If you roll the losing target before rolling either guess1 or guess2, you lose, if not, you win and double your money...

I am struggling to make it so that the order of die rolled doesn't matter and it doesn't double count numbers.

I am also struggling to make a while loop for the losing target situation... please help! Very new to this.

def play(guess1:int,guess2:int, dollars:int):
    
    random_die1 = roll_one_die() ##calling helper function 
    random_die2 = roll_one_die() 
    win_count = 0 ## these are not correct, not sure how to properly account for different rolls
    if guess1 == random_die1:
        win_count = 1
    else:
        win_count = 0
    
    if guess1 == random_die2:
        if win_count==1:
            win_count = 1
        else:
            win_count = 1
               
    if guess2 == random_die1:
        if guess1 != random_die1 and guess1 != random_die2:
            win_count = 1
    else:
        win_count  = 0
    if guess2 == random_die2:
        if guess2 != random_die1 and guess2 != random_die2:
            win_count = 1
        #if win_count==2:
            #win_count=2
        else:
            win_count  =0 

    if win_count ==2:
        dollars=dollars*3
    elif win_count ==0:
        dollars=0
    else:
        losing_target= random_die1 random_die2
        new_roll1 = roll_one_die()
        new_roll2 = roll_one_die()
        sum_of = (new_roll1   new_roll2)
        if losing_target==sum_of:
            dollars==0        
        while sum_of!= losing_target: ## not sure how to properly loop so first situation = win or loss
            sum_of == new_roll1   new_roll2
            if new_roll1 == guess1 or new_roll1 == guess2 or new_roll2 == guess1 or new_roll2 == guess2:
                dollars=dollars*2
        return(dollars)

CodePudding user response:

I am reluctant to just post a complete solution because I am sure that it would be better for you to struggle with this until you solved it yourself.

For the losing target situation, surely you need to be rolling the dice inside the while loop, for example

while True:
    new_roll1 = roll_one_die()
    new_roll2 = roll_one_die()
    sum_of = new_roll1   new_roll2
    if losing_target == sum_of:
        dollars = 0
        break        
    if new_roll1 == guess1 or new_roll1 == guess2 or new_roll2 == guess1 or new_roll2 == guess2:
        dollars = dollars * 2
        break

Untested.

  • Related