Level 05: one-dimension arrays and lists 0 of 9 activities solved
  1. 1. RPG inventory Not yet solved
  2. 2. Search for life Not yet solved
  3. 3. Two-dice pig Not yet solved
  4. 4. Random name generator Not yet solved
  5. 5. Cup draw Not yet solved
  6. 6. Feud Not yet solved
  7. 7. Darts Not yet solved
  8. 8. Cut the deck Not yet solved
  9. 9. London underground Not yet solved
Next series Level 06: count controlled iterations and scope
Level 05: one-dimension arrays and lists

3. Two-dice pig

Sign in to hand in your solution and get feedback.

# Two-dice pig program # ------------------------- # Import libraries # ------------------------- import random # ------------------------- # Subprograms # ------------------------- def roll(): dice = [0, 0] dice[0] = random.randint(1, 6) dice[1] = random.randint(1, 6) return dice def points(dice): if dice[0] == 1 and dice[1] == 1: return -1 elif dice[0] == 1 or dice[1] == 1: return 0 else: return dice[0] + dice[1] def play_game(): random.seed() score = [0, 0] player = 0 new_player = True while score[0] < 100 and score[1] < 100: if new_player: print() print("------------------------------------") print("Player", player + 1, "it's your turn.") print("Your score is currently:", score[player]) total = 0 new_player = False turn_end = False print("Press Enter to roll the dice.") input() dice = roll() result = points(dice) print("You rolled a", dice[0], "and", dice[1]) if result > 0: print("You scored", result) total = total + result print("Your total is", total) choice = "" while choice not in ["y", "n"]: choice = input("Do you want to continue y/n? :") if choice == "n": score[player] = score[player] + total turn_end = True else: print("Oh no, that's a pig out!") turn_end = True if turn_end: print("Press Enter to hand the dice to the next player.") input() player = (player + 1) % 2 new_player = True # ------------------------- # Main program # ------------------------- play_game()