Level 05: one-dimension arrays and lists 0 van 9 activiteiten opgelost
  1. 1. RPG inventory Nog niet opgelost
  2. 2. Search for life Nog niet opgelost
  3. 3. Two-dice pig Nog niet opgelost
  4. 4. Random name generator Nog niet opgelost
  5. 5. Cup draw Nog niet opgelost
  6. 6. Feud Nog niet opgelost
  7. 7. Darts Nog niet opgelost
  8. 8. Cut the deck Nog niet opgelost
  9. 9. London underground Nog niet opgelost
Volgende reeks Level 06: count controlled iterations and scope
Level 05: one-dimension arrays and lists

3. Two-dice pig

Meld je aan om je oplossing in te dienen en feedback te krijgen.

# 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()