Advertisement
Cobble5tone

Educational loops and nested loops program

Aug 1st, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.93 KB | None | 0 0
  1. """
  2. This is educational code for a 2 player game that looks at who has the highest dice roll. Each player must roll a die
  3. five times and have that total saved to a dictionary. Then we need to repeat that procedure 10 times over. After that,
  4. we must go through our dictionary and see who won each roll!
  5. Python 3.6
  6.  
  7. """
  8. player_one_roll_totals = {}  # Holds our totals for player 1
  9. player_two_roll_totals = {}  # Holds our totals for player 2
  10.  
  11. import random
  12.  
  13. playerOne = input('What is player 1\'s name?')  # We take in our players names here
  14. playerTwo = input('What is player 2\'s name?')
  15.  
  16. print('Welcome to the game ' + playerOne + ' and ' + playerTwo)  # Greeting message...just because
  17.  
  18. counter = 0  # see below
  19. # We set a variable called counter, which will be used to control our 'while' loop we're about to run
  20. # if no variable was set, then the code would run without pause as there would be no escape condition to make the
  21. # while loop false.
  22. # The while loop below is the ten times that we need to repeat rolling 5 dice
  23. while counter < 10:  # here we tell the computer to check and if counter is less than 10 if so, then loop again
  24.     new_key = counter + 1  # This could be thought of as which round we're on. It also plays a role later in dynamically
  25.     # adding the round to our dictionary key names.
  26.    
  27.     """
  28.    Okay, so, we're already in a loop at this point in the code because we need to repeat the same procedure 10 times.
  29.    However, we now need to have what you might call a sub-procedure. That is to say, we need to roll a die 5 times
  30.    first. So we have a loop within a loop. This is known as a nested loop.
  31.    
  32.    Alright, below we're starting our nested loops! So, per our instructions, we need to roll a single dice 5 times.
  33.    the code below is mini loop for player one to roll their dice 5 times. The playerOneCounter is a way of keeping
  34.    track how many times the loop has gone through. We want to go  through 5 times. So, this code starting at
  35.    'while playerOnecounter < 5:' starts by rolling a random number between 1 and 6 it then adds that number to
  36.    'playerOneTotal' so, after five rounds you have the sum of all five roles. Notice again the playerOneCounter, which
  37.    adds one each time we go around to ensure we stop at 5.
  38.    """
  39.     playerOneCounter = 0  # How many times player 1 has rolled the dice
  40.     playerOneTotal = 0  # The total of the dice
  41.     while playerOneCounter < 5:  # Once we roll 5 times this wont run anymore
  42.         playerOneRoll = random.randint(1, 6)  # Generates a random number between 1-6
  43.         playerOneTotal += playerOneRoll  # Adds whatever number is randomly generated to playerOneTotal
  44.         playerOneCounter += 1  # Adds one to show we've rolled the dice one more time
  45.  
  46.     """
  47.    At this point in the code, we've shaken the dice 5 times. Our total is captured in playerOneTotal. That number is
  48.    then appended to our dictionary called player_one_roll_totals. The key of the dictionary is the roll number, which
  49.    is created by adding the  word roll to 'new_key' which is our counter number. The value is playerOneTotal. They are
  50.    now saved in the dictionary for later.
  51.    """
  52.     new_key_creation = 'roll ' + str(new_key)  # Adds the word roll to how many times we've gone through the loop
  53.     player_one_roll_totals[new_key_creation] = playerOneTotal  # Adds our data to the dictionary
  54.    
  55.     """
  56.    At this point, we do  the same thing we did for player one, but for player two. The code is identical in logic. The
  57.    only difference is that this data gets saved to the dictionary player_two_roll_totals. I'm not going to comment
  58.    on each part as it's truly the same process again just with different names.
  59.    
  60.    """
  61.  
  62.     playerTwoCounter = 0
  63.     playerTwoTotal = 0
  64.     while playerTwoCounter < 5:
  65.         playerTwoRoll = random.randint(1, 6)
  66.         playerTwoTotal += playerTwoRoll
  67.         playerTwoCounter += 1
  68.  
  69.     new_key_creation = 'roll ' + str(new_key)
  70.     player_two_roll_totals[new_key_creation] = playerTwoTotal
  71.  
  72.     """
  73.    At this point we've rolled the dice for each player 1 time. The counter below adds 1 to our counter. We now go back
  74.    to the top and repeat this whole thing over again because we need to run it ten times per our instructions.
  75.    """
  76.     counter += 1  
  77.  
  78.  
  79.  
  80. """
  81. So, here we are, we've run the previous loop 10 times with our mini loops running to roll for each player. We now have
  82. two dictionaries loaded with data and ready to be compared. Below, prints off all the numbers in each dictionary for
  83. each player.
  84.  
  85. """
  86. print('Player 1 final totals:')
  87. result_player_one = "".join(' ' + str(value) for key, value in player_one_roll_totals.items())
  88. print(result_player_one)
  89. print('Player 2 final totals:')
  90. result_player_two = "".join(' ' + str(value) for key, value in player_two_roll_totals.items())
  91. print(result_player_two)
  92.  
  93. """
  94. I know below looks really, really dicey. It's not as bad as it looks though. So, here's what's happening in plain
  95. English. We have two dictionaries and we need to go through both of them. The first two lines are essentially the same
  96. thing. It says for keys, values in player_one_roll_totals and for keys, values in player_two_roll_totals
  97. which is basically telling  the computer to go through the data we've saved at this point.  Next, we tell the computer
  98. to compare the roll numbers. In other words, we want to make sure that we're comparing the proper numbers together. Like
  99. player 1s first roll to player 2s first roll and not to player 2s last roll. The computer first looks at player ones
  100. dictionary and says 'okay roll 1' then it looks at player 2s dictionary and says ' roll 1' it then compares the values
  101. to see which is greater. It then prints off the roll number that was compared and who the victor was! The computer
  102. compares each piece of data that way. Until it runs out thus letting us know who's won each round!
  103.  
  104. """
  105.  
  106. for player_one_roll_number, player_one_roll_value in player_one_roll_totals.items():
  107.     for player_two_roll_number, player_two_roll_value in player_two_roll_totals.items():
  108.         if player_one_roll_number == player_two_roll_number:
  109.             if player_one_roll_value > player_two_roll_value:
  110.                 print(str(player_one_roll_number) + ' Player 1 is the Winner!')
  111.             elif player_two_roll_value > player_one_roll_value:
  112.                 print(str(player_two_roll_number) + ' Player 2 is the Winner!')
  113.             else:
  114.                 print("It's a tie!")
  115.  
  116.  
  117.  
  118. """
  119. To help you see what we end up with. The two print statements below will print off the whole dictionaries once they're
  120. completed. This will show you how we built our data. I know this whole thing seems overwhelming, but I assure you that
  121. it's just a lot of noise. There's really only a few key principals at play:
  122.  
  123. 1) Loops - We have a main loop and nested loops. This is where most people's heads will spin. It's hard to grasp having
  124. one process cycling then starting a sub-process. Imagine it like this though. You're bagging groceries. First you fill
  125. up a bag, then you load that bag into your cart. You then repeat the process of filling a bag until it is full. You then
  126. add it to your cart. This is the essence of loops.
  127.  
  128. 2) Iteration - Iteration is a key word anytime you're going through anything in programming. It doesn't matter what data
  129. type you're dealing with. If you need to go through it, then you're iterating. Imagine the shopping metaphor again.
  130. Say you've finished packing your groceries in your cart, but then realize you put your money in one of the bags! Oh, the
  131. horror! You will now need to iterate through the cart and through the  bags. You start by grabbing one bag, then you
  132. iterate through the items. Then you go on to  the next bag until you've completed the task
  133.  
  134. That's more or less what we're doing with our dictionaries, but with a few more comparisons.
  135.  
  136.  
  137. """
  138. print(player_one_roll_totals)
  139. print(player_two_roll_totals)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement