|
6 | 6 | And so on for other cases |
7 | 7 | """ |
8 | 8 |
|
| 9 | +# you can use this code also, see this code is very short in compare to your code |
| 10 | +# code starts here |
| 11 | +""" |
| 12 | +# Snake || Water || Gun __ Game |
| 13 | +
|
| 14 | +import random |
| 15 | +
|
| 16 | +times = 10 # times to play game |
| 17 | +
|
| 18 | +comp_choice = ["s","w","g"] # output choice for computer |
| 19 | +
|
| 20 | +user_point = 0 # user point is initially marked 0 |
| 21 | +comp_point = 0 # computer point is initially marked 0 |
| 22 | +
|
| 23 | +
|
| 24 | +while times >= 1: |
| 25 | + comp_rand = random.choice(comp_choice) # output computer will give |
| 26 | + # |
| 27 | + # print(comp_rand) # checking if the code is working or not |
| 28 | +
|
| 29 | + print(f"ROUND LEFT = {times}") |
| 30 | +
|
| 31 | +# checking if the input is entered correct or not |
| 32 | + try: |
| 33 | + user_choice = input("Enter the input in lowercase ex. \n (snake- s) (water- w) (gun- w)\n:- ") # user choice, the user will input |
| 34 | + except Exception as e: |
| 35 | + print(e) |
| 36 | +
|
| 37 | +# if input doen't match this will run |
| 38 | + if user_choice != 's' and user_choice != 'w' and user_choice != 'g': |
| 39 | + print("Invalid input, try again\n") |
| 40 | + continue |
| 41 | +
|
| 42 | +# checking the input and calculating score |
| 43 | + if comp_rand == 's': |
| 44 | + if user_choice == 'w': |
| 45 | + comp_point += 1 |
| 46 | + elif user_choice == 'g': |
| 47 | + user_point += 1 |
| 48 | + |
| 49 | + elif comp_rand == 'w': |
| 50 | + if user_choice == 'g': |
| 51 | + comp_point += 1 |
| 52 | + elif user_choice == 's': |
| 53 | + user_point += 1 |
| 54 | + |
| 55 | + elif comp_rand == 'g': |
| 56 | + if user_choice == 's': |
| 57 | + comp_point += 1 |
| 58 | + elif user_choice == 'w': |
| 59 | + user_point += 1 |
| 60 | + |
| 61 | + times -=1 # reducing the number of rounds after each match |
| 62 | +
|
| 63 | +if user_point>comp_point: # if user wins |
| 64 | + print(f"WOOUUH! You have win \nYour_point = {user_point}\nComputer_point = {comp_point}") |
| 65 | +elif comp_point>user_point: # if computer wins |
| 66 | + print(f"WE RESPECT YOUR HARD WORK, BUT YOU LOSE AND YOU ARE A LOSER NOW! \nYour_point = {user_point}\nComputer_point = {comp_point}") |
| 67 | +elif comp_point==user_point: # if match draw |
| 68 | + print(f"MATCH DRAW\nYour_point = {user_point}\nComputer_point = {comp_point}") |
| 69 | +
|
| 70 | +else: # just checked |
| 71 | + print("can't calculate score") |
| 72 | +
|
| 73 | +exit = input("PRESS ENTER TO EXIT") |
| 74 | +
|
| 75 | +""" # code ends here |
9 | 76 | import random |
10 | 77 | import time |
11 | 78 |
|
|
28 | 95 | com_choice = random.choice(list(choices.keys())).lower() |
29 | 96 | user_choice = input('\n----->').lower() |
30 | 97 | print("Mr. Computer's choice is : " + com_choice) |
31 | | - |
32 | | - if user_choice == 's' and com_choice == 'w': |
33 | | - print("\n-------Mr. Computer won this round--------") |
34 | | - com_win += 1 |
35 | | - |
36 | | - elif user_choice == 's' and com_choice == 'g': |
37 | | - print("\n-------Mr. Computer won this round--------") |
38 | | - com_win += 1 |
39 | | - |
40 | | - elif user_choice == 'w' and com_choice == 's': |
41 | | - print("\n-------You won this round-------") |
42 | | - user_win += 1 |
43 | | - |
44 | | - elif user_choice == 'g' and com_choice == 's': |
45 | | - print("\n-------You won this round-------") |
46 | | - user_win += 1 |
47 | | - |
48 | | - elif user_choice == 'g' and com_choice == 'w': |
49 | | - print("\n-------Mr. Computer won this round--------") |
50 | | - com_win += 1 |
51 | | - |
52 | | - elif user_choice == 'w' and com_choice == 'g': |
53 | | - print("\n-------You won this round-------") |
54 | | - user_win += 1 |
55 | | - |
56 | | - elif user_choice == com_choice: |
57 | | - print("\n-------This round was a draw-------") |
58 | | - match_draw += 1 |
59 | | - |
60 | | - else: |
61 | | - print('\n\nYou entered wrong !!!!!!') |
62 | | - x = 0 |
63 | | - print('Restarting the game') |
64 | | - print('') |
65 | | - time.sleep(1) |
66 | | - continue |
| 98 | + |
| 99 | + # you can use this code to minimize your writing time for the code |
| 100 | + """ |
| 101 | + if comp_rand == 's': |
| 102 | + if user_choice == 'w': |
| 103 | + print("\n-------Mr. Computer won this round--------") |
| 104 | + comp_point += 1 |
| 105 | + elif user_choice == 'g': |
| 106 | + print("\n-------You won this round-------") |
| 107 | + user_point += 1 |
| 108 | + else: |
| 109 | + match_draw +=1 |
| 110 | + |
| 111 | + elif comp_rand == 'w': |
| 112 | + if user_choice == 'g': |
| 113 | + print("\n-------Mr. Computer won this round--------") |
| 114 | + comp_point += 1 |
| 115 | + elif user_choice == 's': |
| 116 | + print("\n-------You won this round-------") |
| 117 | + user_point += 1 |
| 118 | + else: |
| 119 | + match_draw +=1 |
| 120 | + |
| 121 | + elif comp_rand == 'g': |
| 122 | + if user_choice == 's': |
| 123 | + print("\n-------Mr. Computer won this round--------") |
| 124 | + comp_point += 1 |
| 125 | + elif user_choice == 'w': |
| 126 | + print("\n-------You won this round-------") |
| 127 | + user_point += 1 |
| 128 | + else: |
| 129 | + match_draw +=1 |
| 130 | + |
| 131 | + """ |
| 132 | + |
| 133 | + if comp_rand == 's': |
| 134 | + if user_choice == 'w': |
| 135 | + print("\n-------Mr. Computer won this round--------") |
| 136 | + comp_point += 1 |
| 137 | + elif user_choice == 'g': |
| 138 | + print("\n-------You won this round-------") |
| 139 | + user_point += 1 |
| 140 | + else: |
| 141 | + match_draw +=1 |
| 142 | + |
| 143 | + elif comp_rand == 'w': |
| 144 | + if user_choice == 'g': |
| 145 | + print("\n-------Mr. Computer won this round--------") |
| 146 | + comp_point += 1 |
| 147 | + elif user_choice == 's': |
| 148 | + print("\n-------You won this round-------") |
| 149 | + user_point += 1 |
| 150 | + else: |
| 151 | + match_draw +=1 |
| 152 | + |
| 153 | + elif comp_rand == 'g': |
| 154 | + if user_choice == 's': |
| 155 | + print("\n-------Mr. Computer won this round--------") |
| 156 | + comp_point += 1 |
| 157 | + elif user_choice == 'w': |
| 158 | + print("\n-------You won this round-------") |
| 159 | + user_point += 1 |
| 160 | + else: |
| 161 | + match_draw +=1 |
| 162 | +# You can use TRY-EXCEPT in here |
| 163 | +""" |
| 164 | +try: |
| 165 | + user_choice = input("Enter the input in lowercase ex. \n (snake- s) (water- w) (gun- w)\n:- ") # user choice, the user will give the input |
| 166 | + except Exception as e: |
| 167 | + print(e) |
| 168 | +""" |
| 169 | + try: |
| 170 | + user_choice = input("Enter the input in lowercase ex. \n (snake- s) (water- w) (gun- w)\n:- ") # user choice, the user will input |
| 171 | + except Exception as e: |
| 172 | + print(e) |
67 | 173 |
|
68 | 174 | x += 1 |
69 | 175 | print('\n') |
|
0 commit comments