├── README.md ├── rps.py └── rps_noif.py /README.md: -------------------------------------------------------------------------------- 1 | # rockpaperscissors 2 | Source code for the two videos I did on programming rock, paper, scissors in python. First file is traditional implementation. Second file uses no if statements. 3 | -------------------------------------------------------------------------------- /rps.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | while True: 4 | print('Make your choice:') 5 | choice = str(input()) 6 | choice = choice.lower() 7 | 8 | print("My choice is", choice) 9 | 10 | choices = ['rock', 'paper', 'scissors'] 11 | 12 | computer_choice = random.choice(choices) 13 | 14 | print("Computer choice is", computer_choice) 15 | if choice in choices: 16 | if choice == computer_choice: 17 | print('it is a tie') 18 | if choice == 'rock': 19 | if computer_choice == 'paper': 20 | print('you lose, sorry :(') 21 | elif computer_choice == 'scissors': 22 | print('You win!!!!! congrats :)') 23 | if choice == 'paper': 24 | if computer_choice == 'scissors': 25 | print('you lose, sorry :(') 26 | elif computer_choice == 'rock': 27 | print('You win!!!!! congrats :)') 28 | if choice == 'scissors': 29 | if computer_choice == 'rock': 30 | print('you lose, sorry :(') 31 | elif computer_choice == 'paper': 32 | print('You win!!!!! congrats :)') 33 | else: 34 | print('invalid choice, try again') 35 | 36 | print() -------------------------------------------------------------------------------- /rps_noif.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | random.seed(42) 4 | 5 | while True: 6 | print("Make your guess:", end=" ") 7 | guess = str(input()) 8 | guess = guess.lower() 9 | print("you guessed", guess) 10 | choices = ['rock', 'paper', 'scissors'] 11 | computer_guess = random.choice(choices) 12 | print("computer guessed", computer_guess) 13 | guess_dict = {'rock': 0, 'paper': 1, 'scissors': 2} 14 | guess_idx = guess_dict.get(guess, 3) 15 | computer_idx = guess_dict.get(computer_guess) 16 | result_matrix = [[0,2,1], 17 | [1,0,2], 18 | [2,1,0], 19 | [3,3,3]] 20 | result_idx = result_matrix[guess_idx][computer_idx] 21 | result_messages = ["it is a tie", "You win!!! Congrats", 'the computer wins :(', 'invalid guess'] 22 | result = result_messages[result_idx] 23 | print(result) 24 | print() --------------------------------------------------------------------------------