0

Can anyone guide me to simplify this primitive entry level code / Rock, paper and scissors

Hey everyone. I just started learning python and made a rock, scissors and paper game by my own with my limited basic level knowledge. But I feel like the codes should be much more simple than this one. Need your guidence to simplify. import random ai_list = ("rock", "scissors", "paper") end_game = False lose_count = 0 win_count = 0 while lose_count < 3: ai_decision = (ai_list[random.randint(1, 3)]) user_decision = input("rock, scissors or paper ? :").lower() while ai_decision == "rock": if user_decision == "rock": print(f'computer:{ai_decision}, user: {user_decision}. Draw game.') break elif user_decision == "scissors": print(f'computer:{ai_decision}, user: {user_decision}. You lost.') lose_count += 1 break elif user_decision == "paper": print(f'computer:{ai_decision}, user: {user_decision}. You won.') win_count += 1 break while ai_decision == "scissors": if user_decision == "rock": print(f'computer:{ai_decision}, user: {user_decision}. You won.') win_count += 1 break elif user_decision == "scissors": print(f'computer:{ai_decision}, user: {user_decision}. Draw game.') break elif user_decision == "paper": print(f'computer:{ai_decision}, user: {user_decision}. You lost.') lose_count += 1 break while ai_decision == "paper": if user_decision == "rock": print(f'computer:{ai_decision}, user: {user_decision}. You lost.') lose_count += 1 break elif user_decision == "scissors": print(f'computer:{ai_decision}, user: {user_decision}. You won.') win_count += 1 break elif user_decision == "paper": print(f'computer:{ai_decision}, user: {user_decision}. Draw game.') break else: print("

13th Oct 2021, 10:54 PM
Serhat Taşdemir
Serhat Taşdemir - avatar
2 ответов
+ 1
#here is (just for reference) how I refactored your code #if you have any doubt please let me know from random import choice # choice randomly chooses an element from a sequence hands = r, p, s = ("rock", "paper", "scissors") #same as r = "rock"; p = "paper"; s = "scissors"; hands = (r, p, s) outcomes = draw, win, lose= ("Draw game", "You won", "You lost") counts = {win: 0, lose: 0, draw: 0} #counts is a dictionary: counts[win] == 0 ... def game(player, computer): #below I take the element (player, computer) of the dictionary #if player == r == "rock" and so does computer, than the dictionary yields #(r, r) -> draw return {(r, r): draw, (s, s): draw, (p, p): draw, (r, s): win, (s, p): win, (p, r): win, (s, r): lose, (r, p): lose, (p, s): lose }[(player, computer)] #we can actually do better (using a bit of math): #return outcomes[hands.index(player)-hands.index(computer)] while counts[lose] < 3: computer = choice(hands) # randomly chooses between r, p and s player = input("rock, scissors or paper ? ").lower() if player not in hands: # if what the user wrote is nither r, p or s # than it does whatever you wanted it to do #and then jumps to the beginning of the while-loop ... continue #else: result = game(player, computer) # now result is either win, lose or draw counts[result] += 1 print (f'{computer = }, {player = }. {result}.')
14th Oct 2021, 4:25 AM
Angelo
Angelo - avatar
0
Thanks mate, I will practice and try to grasp the concept of this version. Appreciated!
14th Oct 2021, 7:39 AM
Serhat Taşdemir
Serhat Taşdemir - avatar