1
- # @author (Aymar N.)
2
- # @version (2021) Version 2.0.0
1
+ from collections import OrderedDict
2
+ import random
3
3
4
- # How to run the program
5
- # Copy and paste the code in codeSkupltor
6
- # https://py2.codeskulptor.org/
4
+ player_number = 0
5
+ comp_number = 0
7
6
7
+ # python interpreter
8
+ # Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep 6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32
8
9
9
- # Rock-paper-scissors-lizard-Spock template
10
- import random
10
+ # @Author(Aymar Sedami NAHUM)
11
+ # @Version(11/01/2024)
11
12
12
- def name_to_number (name ):
13
- """a helper function that converts name into a number"""
14
-
15
- if name == "rock" :
16
- name = 0
17
- return name
18
- elif name == "paper" :
19
- name = 1
20
- return name
21
- elif name == "scissors" :
22
- name = 2
23
- return name
24
- elif name == "lizard" :
25
- name = 3
26
- return name
27
- elif name == "Spock" :
28
- name = 4
29
- return name
30
- else :
31
- return "Error wrong string"
32
-
13
+ # Step 1. and Step 2.
14
+ def name_to_number ():
33
15
34
- def number_to_name (number ):
35
- """converts a number into its matching String"""
36
- if number == 0 :
37
- number = "rock"
38
- return number
39
- elif number == 1 :
40
- number = "paper"
41
- return number
42
- elif number == 2 :
43
- number = "scissors"
44
- return number
45
- elif number == 3 :
46
- number = "lizard"
47
- return number
48
- elif number == 4 :
49
- number = "Spock"
50
- return number
51
- else :
52
- return "Error wrong number"
53
-
54
-
55
- def rpsls (player_choice ):
56
- """ Generates the computer's guess and print a message """
57
- """ Determines and prints out the winner """
58
- # print a blank line to differents games
16
+ """ From a helper function that converts name into a number to a dict"""
17
+
18
+ dict_name_to_number = OrderedDict ()
19
+ dict_name_to_number ["rock" ] = 0
20
+ dict_name_to_number ["paper" ] = 1
21
+ dict_name_to_number ["scissors" ] = 2
22
+ dict_name_to_number ["lizard" ] = 3
23
+ dict_name_to_number ["spock" ] = 4
24
+
25
+ #for key in dict_name_to_number.items():
26
+ # print(key)
27
+
28
+ return dict_name_to_number
29
+
30
+ # Step 3.
31
+ def rpsls_first_part ():
32
+ global player_number
33
+ choices = ["ROCK" , "PAPER" , "SPOCK" , "LIZARD" , "SCISSORS" ]
34
+ print (" " )
35
+
36
+ print ("Dear Player" )
37
+ print ("Choose between those five : " )
38
+ print ("rock, spock, paper, lizard, scissors" )
39
+
40
+ player_choice = input (" " )
41
+
59
42
print ("" )
60
- # print out the message for the player's choice
61
- print "Player chooses" ,player_choice
62
- # convert the player's choice to player_number
63
- #using the function name_to_number()
64
- player_number = name_to_number (player_choice )
65
- # compute random guess for comp_number using
66
- #random.randrange()
67
- comp_number = random .randrange (0 ,5 )
68
- # convert comp_number to comp_choice using
69
- #the function number_to_name()
70
- comp_choice = number_to_name (comp_number )
71
- # print out the message for computer's choice
72
- print "Computer chooses" ,comp_choice
73
- # compute difference of comp_number and
74
- #player_number modulo five
75
- compute = (comp_number - player_number )% 5
76
- # use if/elif/else to determine winner,
77
- #print winner message
78
- if (comp_number == player_number ):
79
- print "Player and computer tie!"
80
- elif (compute == 3 or compute == 4 ):
81
- print "Computer wins!"
82
- elif (compute == 1 or compute == 2 ):
83
- print "Player wins!"
84
- else :
85
- print "Error"
86
-
87
-
88
- # test your code - THESE CALLS MUST BE PRESENT IN YOUR
89
- #SUBMITTED CODE
90
- rpsls ("rock" )
91
- rpsls ("Spock" )
92
- rpsls ("paper" )
93
- rpsls ("lizard" )
94
- rpsls ("scissors" )
43
+ print (player_choice )
44
+
45
+ # then compute the number
46
+
47
+ try :
48
+
49
+ for k , v in name_to_number ().items ():
50
+ if choices .index (player_choice .upper ()) == - 1 :
51
+ print ("That choice is not in the rpsls list" )
52
+
53
+ except ValueError as e :
54
+
55
+ print ("That choice is not allowed" )
56
+
57
+ for k , v in name_to_number ().items ():
58
+ if k == player_choice :
59
+ player_number = name_to_number ()[k ]
60
+ print ("player_number : " + str (player_number ))
61
+ return player_number
62
+
63
+ rpsls_first_part ()
64
+
65
+ # Step 4.
66
+ def rpsls_second_part ():
67
+ global comp_number
68
+ print (" " )
69
+ print (" The computer guess is : " )
70
+ comp_number = random .randrange (5 )
71
+ for value in name_to_number ().values ():
72
+ if comp_number == value :
73
+ print (comp_number )
74
+ return comp_number
75
+
76
+ rpsls_second_part ()
77
+
78
+ # Spep 5.
79
+ def rpsls_last_part ():
80
+ global comp_number
81
+ global player_number
82
+
83
+ print (" " )
84
+
85
+ if ((comp_number - player_number ) == 0 ):
86
+ print ("That is a tie. Try again " )
87
+
88
+ if (comp_number > player_number ):
89
+ if (comp_number - player_number <= 2 ):
90
+ print ("The computer wins. " )
91
+ #elif:
92
+ else :
93
+ print (" You wins !!! " )
94
+
95
+ elif (player_number > comp_number ):
96
+ if (player_number - comp_number <= 2 ):
97
+ print (" You wins !!! " )
98
+ #elif:
99
+ else :
100
+ print (" The computer wins. " )
101
+
102
+ rpsls_last_part ()
103
+
104
+
95
105
96
- # always remember to check your completed program against
97
- #the grading rubric
106
+
107
+
108
+
109
+
0 commit comments