Advertisement
Cobble5tone

Dice game

Aug 1st, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. """
  2. This is educational code for a 2 player game that looks at who has the highest dice roll.
  3. Python 3.6
  4.  
  5. """
  6.  
  7. import random
  8.  
  9. playerOne = input('What is player 1\'s name?')  # We take in our players names here
  10. playerTwo = input('What is player 2\'s name?')
  11.  
  12. print('Welcome to the game ' + playerOne + ' and ' + playerTwo)  # Greeting message...just because
  13.  
  14. counter = 0  # see below
  15. # We set a variable called counter, which will be used to control our 'while' loop we're about to run
  16. # if no variable was set, then the code would run without pause as there would be no escape condition to make the
  17. # while loop false.
  18.  
  19. while counter < 10:  # here we tell the computer to check and if counter is less than 10 if so, then loop again
  20.     playerOneRoll = random.randint(0, 6)
  21.     playerTwoRoll = random.randint(0, 6)
  22.     round = counter + 1  # because we start the counter at 0 in order for the program to print 1 I add one here
  23.  
  24.     print('Round: ' + str(round))  # print the round we're on
  25.     print(playerOne + ' rolled: ' + str(playerOneRoll))
  26.     print(playerTwo + ' rolled: ' + str(playerTwoRoll))
  27.  
  28.     if playerOneRoll > playerTwoRoll:
  29.         print(playerOne + ' Wins!\n')
  30.     elif playerTwoRoll > playerOneRoll:
  31.         print(playerTwo + ' Wins!\n')
  32.     else:
  33.         print('You tied!\n')
  34.  
  35.     counter += 1  # very important to add 1 to the counter each time. if you didn't have this, then the program would
  36.     # read counter at 0 and would  never make it to ten. This adds one each time we go through the loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement