Skip to content

Commit d0a1ebe

Browse files
authored
Update rps_part_three.rst
fixed bringing it all together code
1 parent f3f7801 commit d0a1ebe

File tree

1 file changed

+136
-127
lines changed

1 file changed

+136
-127
lines changed

rps_part_three.rst

+136-127
Original file line numberDiff line numberDiff line change
@@ -296,152 +296,161 @@ Bringing it all together!!
296296

297297
.. code-block:: python
298298
299-
# our constants
300-
CPU_OPTIONS = ['rock', 'paper', 'scissors']
301-
USER_OPTIONS = [1,2,3]
302-
GAME_RULES = {
299+
import random, time
300+
301+
# global
302+
name = ""
303+
304+
# our constants
305+
CPU_OPTIONS = ['rock', 'paper', 'scissors']
306+
USER_OPTIONS = [1, 2, 3]
307+
GAME_RULES = {
303308
1: {'strong': 'scissors', 'weak': 'paper'},
304309
2: {'strong': 'rock', 'weak': 'scissors'},
305310
3: {'strong': 'paper', 'weak': 'rock'}
306-
}
311+
}
307312

308-
# game menu
309-
def game_menu():
310-
logged = False
311-
options = [1,2]
312-
313-
while not logged:
314-
# type casing to make sure we get int
315-
menu_selection = int(input(f"""
316-
Welcome {name}, what would you like to do?
317-
1. Play Game
318-
2. Exit Game
319-
"""))
320-
321-
# many ways to create defence
322-
if menu_selection not in options:
323-
print("Please select either 1 or 2")
324-
logged = False
325-
else:
326-
logged = True
327-
return menu_selection
328313

329-
def find_winner(user_selection):
314+
# game menu
315+
def game_menu():
316+
logged = False
317+
options = [1, 2]
330318

331-
# use for loop to start game
332-
for item in CPU_CHOICES:
333-
print(f"{item.capitalize()}...")
334-
time.sleep(1)
319+
# get name
320+
global name # we need to call the global variable before using it
321+
name = input("Welcome to Rock, Paper, Scissors! Please Enter Your Name: ").capitalize()
322+
print()
335323

336-
# get cpu option
337-
cpu = random.choice(CPU_CHOICES)
324+
while not logged:
325+
# type casing to make sure we get int
326+
menu_selection = int(input(f"""
327+
Welcome {name}, what would you like to do?
328+
1. Play Game
329+
2. Exit Game
330+
"""))
338331

339-
# show each option
340-
# since we're using numbers, we can index with a list
341-
print(f"{name} - {CPU_CHOICES[user_selection - 1]} vs CPU - {cpu}")
342-
print("-------------------------------------")
332+
# many ways to create defence
333+
if menu_selection not in options:
334+
print("Please select either 1 or 2")
335+
logged = False
336+
else:
337+
logged = True
338+
return menu_selection
339+
340+
341+
def find_winner(user_selection):
342+
# use for loop to start game
343+
for item in CPU_OPTIONS:
344+
print(f"{item.capitalize()}...")
345+
time.sleep(1)
346+
347+
# get cpu option
348+
cpu = random.choice(CPU_OPTIONS)
349+
350+
# show each option
351+
# since we're using numbers, we can index with a list
352+
print(f"{name} - {CPU_OPTIONS[user_selection - 1]} vs CPU - {cpu}")
353+
print("-------------------------------------")
354+
355+
# win
356+
if GAME_RULES[user_selection]['strong'] == cpu:
357+
return "*** You Win!!! ***", 1
358+
359+
# lose
360+
elif GAME_RULES[user_selection]['weak'] == cpu:
361+
return "--- You Lose! --- :(", 2
362+
363+
# tie
364+
else:
365+
return "^^^ It's a Tie!! ^^^ ", 0
366+
367+
368+
def replay():
369+
menu_options = [1, 2]
370+
confirmed = False # user has not selected option
371+
372+
# play again or exit
373+
while not confirmed:
374+
option = int(input("""
375+
Please Select:
376+
1. Continue Playing
377+
2. Stop Playing and Exit"""))
378+
379+
if option not in menu_options:
380+
print("Please select either 1 or 2")
381+
confirmed = False
343382

344-
345-
# win
346-
if GAME_RULES[user]['strong'] == cpu:
347-
return "*** You Win!!! ***", 1
348-
349-
# lose
350-
elif GAME_RULES[user]['weak'] == cpu:
351-
return "--- You Lose! --- :(", 2
352-
353-
# tie
354383
else:
355-
return "^^^ It's a Tie!! ^^^ ", 0
384+
confirmed = True
385+
return option
356386

357-
def replay():
358-
menu_options = [1,2]
359-
confirmed = False # user has not selected option
360-
361-
# play again or exit
362-
while not confirmed:
363-
option = int(input("""
364-
Please Select:
365-
1. Continue Playing
366-
2. Stop Playing and Exit"""))
367-
368-
369-
if option not in menu_options:
370-
print("Please select either 1 or 2")
371-
confirmed = False
372387

388+
def play_game():
389+
# counters for keeping game score
390+
win = 0
391+
loss = 0
392+
draw = 0
393+
394+
# we want to play multiple rounds
395+
playing = True
396+
397+
while playing:
398+
# type casting
399+
user_selection = int(input("""Please Select One:
400+
1. Rock
401+
2. Paper
402+
3. Scissors"""))
403+
404+
# verify
405+
if user_selection not in USER_OPTIONS:
406+
print("Please select either 1 or 2")
407+
continue # top of the loop
408+
409+
else:
410+
411+
# save results in a variable (will be a tuple)
412+
results = find_winner(user_selection)
413+
414+
# game results
415+
# win
416+
if results[1] == 1:
417+
win += 1
418+
# loss
419+
elif results[1] == 2:
420+
loss += 1
421+
# draw
373422
else:
374-
confirmed = True
375-
return option
423+
draw += 1
376424

377-
def play_game():
425+
print(results[0]) # show game result
426+
# show current record
427+
print(f"""
428+
Current Record:
429+
Wins - {win} | Losses {loss} | Draws {draw}""")
378430

379-
# counters for keeping game score
380-
win = 0
381-
loss = 0
382-
draw = 0
383-
384-
# we want to play multiple rounds
385-
playing = True
386-
387-
while playing:
388-
# type casting
389-
user_selection = int(input("""Please Select One:
390-
1. Rock
391-
2. Paper
392-
3. Scisoors"""))
431+
# run replay function
432+
replay_choice = replay()
393433

394-
# verify
395-
if user_selection not in USER_OPTIONS:
396-
print("Please select either 1 or 2")
397-
continue # top of the loop
398-
434+
# restart from top of the while loop/ play again
435+
if replay_choice == 1:
436+
continue
399437
else:
400-
401-
# save results in a variable (will be a tuple)
402-
results = find_winner(user_selection)
403-
404-
405-
# game results
406-
# win
407-
if result[1] == 1:
408-
win += 1
409-
# loss
410-
elif result[1] == 2:
411-
loss += 1
412-
# draw
413-
else:
414-
draw += 1
415-
416-
print(result[0]) # show game result
417-
# show current record
418-
print(f"""
419-
Current Record:
420-
Wins - {win} | Losses {loss} | Draws {draw}""")
421-
422-
# run replay function
423-
replay_choice = replay()
424-
425-
# restart from top of the while loop/ play again
426-
if option == 1:
427-
continue
428-
else:
429-
playing = False
430-
print(f"Bye {name}, thanks For Playing!")
431-
exit()
438+
playing = False
432439

433-
game_on = True
440+
print(f"Bye {name}, thanks For Playing!")
441+
exit()
434442

435-
while game_on:
436-
game_option = game_menu()
437443

438-
# play game
439-
if game_option == 1:
440-
play_game()
444+
game_on = True
445+
while game_on:
446+
game_option = game_menu()
447+
# play game
448+
if game_option == 1:
449+
play_game()
450+
441451
# stop playing
442-
else:
443-
print(f"Bye {name}, thanks For Playing!")
444-
game_on = False
445-
446-
exit()
452+
else:
453+
print(f"Bye {name}, thanks For Playing!")
454+
game_on = False
455+
exit()
447456

0 commit comments

Comments
 (0)