@@ -296,161 +296,161 @@ Bringing it all together!!
296
296
297
297
.. code-block :: python
298
298
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 = {
308
- 1: {'strong': 'scissors', 'weak': 'paper'},
309
- 2: {'strong': 'rock', 'weak': 'scissors'},
310
- 3: {'strong': 'paper', 'weak': 'rock'}
311
- }
312
-
313
-
314
- # game menu
315
- def game_menu():
316
- logged = False
317
- options = [1, 2]
318
-
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()
323
-
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
- """))
331
-
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
382
-
383
- else:
384
- confirmed = True
385
- return option
386
-
387
-
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
-
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 = {
308
+ 1 : {' strong' : ' scissors' , ' weak' : ' paper' },
309
+ 2 : {' strong' : ' rock' , ' weak' : ' scissors' },
310
+ 3 : {' strong' : ' paper' , ' weak' : ' rock' }
311
+ }
312
+
313
+
314
+ # game menu
315
+ def game_menu ():
316
+ logged = False
317
+ options = [1 , 2 ]
318
+
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 ()
323
+
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
+ """ ))
331
+
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
409
364
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
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
382
+
422
383
else :
423
- draw += 1
424
-
425
- print(results[0]) # show game result
426
- # show current record
427
- print(f"""
428
- Current Record:
429
- Wins - {win} | Losses {loss} | Draws {draw}""")
430
-
431
- # run replay function
432
- replay_choice = replay()
433
-
434
- # restart from top of the while loop/ play again
435
- if replay_choice == 1:
436
- continue
384
+ confirmed = True
385
+ return option
386
+
387
+
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
+
437
409
else :
438
- playing = False
439
-
440
- print(f"Bye {name}, thanks For Playing!")
441
- exit()
442
-
443
-
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
-
451
- # stop playing
452
- 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
422
+ else :
423
+ draw += 1
424
+
425
+ print (results[0 ]) # show game result
426
+ # show current record
427
+ print (f """
428
+ Current Record:
429
+ Wins - { win} | Losses { loss} | Draws { draw} """ )
430
+
431
+ # run replay function
432
+ replay_choice = replay()
433
+
434
+ # restart from top of the while loop/ play again
435
+ if replay_choice == 1 :
436
+ continue
437
+ else :
438
+ playing = False
439
+
453
440
print (f " Bye { name} , thanks For Playing! " )
454
- game_on = False
455
- exit()
456
-
441
+ exit ()
442
+
443
+
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
+
451
+ # stop playing
452
+ else :
453
+ print (f " Bye { name} , thanks For Playing! " )
454
+ game_on = False
455
+ exit ()
456
+
0 commit comments