Home > Back-end >  How do I keep count of how many times something has been printed?
How do I keep count of how many times something has been printed?

Time:10-10

My goal is to print out a while loop for two dice rolling, and they will continue to roll until both dice are 1 and 1, in which case they are both Odd numbers, are Doubles. However, if they roll any other number, say 2 and 2 for example, it would be considered Evens, and Doubles. At the end, I want to keep a count of how many times the dice has rolled until both roll 1s, and how often Odds, Evens, and Doubles has occurred. I'll just include a section of the code, since the whole program is too lengthy.

I'm not sure where to begin, and I'm new to python.

condition2 = False

while condition2 == False:
    count = 0

    die1 = random.randint(1,sides 1)
    die2 = random.randint(1,sides 1)

   
    evens = "Evens!"
    odds = "Odds!"
    doubles = "Doubles!"
    snake_eye = "Snake Eyes!"
    sum_value = "Sum is size value!"
    highroll = "High Roll!"
    high_low_roll = "High/Low Roll!"
    low_high_roll = "Low/High Roll!"

    if die1 % 2 == 0 and die2 % 2 == 0: #For the even numbers
        if die1 == die2 and die1   die2 == sides: #For even numbers that add up to sum value
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles, sum_value)
        elif die1 == 6 and die2 == 6: #For high roll
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles, highroll)
        elif die1   die1 == die2: #Even numbers that are equal
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles)
        else: #Even numbers that are simply even, nothing else
            print("die #1 is", "*"   str(die1)   "*", "and die #2 is", "*"   str(die2)   "*", evens)

CodePudding user response:

You should just define counters for each situation; for example, you need counters for counting even , odd , doubles , snake_eye, sum_value , highroll , and etc , events.
It seems you didn't implement the Odd part of the program, It is so simple, you just need to add an else for odd numbers and with a little change in the even part, you can make it suitable for odds.

import random

sides = 6
while True: 

    die1 = random.randint(1,sides 1)
    die2 = random.randint(1,sides 1)

    # Initialize the counters for each situation
    even_counter = 0
    odds_counter = 0 
    doubles_counter = 0
    sanke_eye_counter = 0
    sum_value_counter = 0
    highroll_counter = 0
    highlow_counter = 0
    lowhigh_counter = 0

    evens = "Evens!"
    odds = "Odds!"
    doubles = "Doubles!"
    snake_eye = "Snake Eyes!"
    sum_value = "Sum is size value!"
    highroll = "High Roll!"
    high_low_roll = "High/Low Roll!"
    low_high_roll = "Low/High Roll!"

    if die1 % 2 == 0 and die2 % 2 == 0: #For the even numbers
        if die1 == die2 and die1   die2 == sides: #For even numbers that add up to sum value
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles, sum_value)
            even_counter  = 1; doubles_counter  = 1; sum_value  = 1
        elif die1 == 6 and die2 == 6: #For high roll
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles, highroll)
            even_counter  = 1; doubles_counter  = 1; highroll_counter  = 1
        elif die1   die1 == die2: #Even numbers that are equal
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", evens, doubles)
            even_counter  = 1; doubles_counter  = 1
        else: #Even numbers that are simply even, nothing else
            print("die #1 is", "*"   str(die1)   "*", "and die #2 is", "*"   str(die2)   "*", evens)
            even_counter  = 1
    else:
        if die1 == 1 and die2 == 1: # End of the rolls
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", odds, doubles, sum_value)
            odds_counter  = 1; doubles_counter  = 1; sum_value  = 1
            break # end of the while loop
        elif die1 == die2 and die1   die2 == sides: #For odd numbers that add up to sum value
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", odds, doubles, sum_value)
            odds_counter  = 1; doubles_counter  = 1; sum_value  = 1
        elif die1   die1 == die2: #Odd numbers that are equal
            print("die #1 is", "*" str(die1) "*", "and die #2 is", "*" str(die2) "*", odds, doubles)
            odds_counter  = 1; doubles_counter  = 1
        else: #Odd numbers that are simply even, nothing else
            print("die #1 is", "*"   str(die1)   "*", "and die #2 is", "*"   str(die2)   "*", odds)
            odds_counter  = 1


  • Related