Aug-11-2019, 12:17 PM
As a third way to do it, I like this dictionary for rock, paper, scissors:
wins = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}It stores what beat what in a slightly different data structure than perfringo used. Again, checking for the win is very simple:
if user_choice == computer_choice: # draw elif wins[user_choice] == computer_choice: # user wins else: # computer winsNote that I like this because it scales well for expanding to rock-paper-scissors-lizard-spock:
wins = {'rock': ['scissors', 'lizard'], 'scissors': ['paper', 'lizard'], 'paper': ['rock', 'spock'], 'lizard': ['paper', 'spock'], 'spock': ['scissors', 'rock']}You just need to change to testing with the in operator:
if user_choice == computer_choice: # draw elif computer_choice in wins[user_choice]: # user wins else: # computer winsThe lesson being that there may be two different algorithms/data structures that are basically equivalent for the problem at hand, but the choice of which to use may be driven by other parts of the application.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures