Python3, please wite the code with comments
2. Write a program to implement the Pokemon game using your algorithm from
It should ask the user to enter their type and compare it with a randomly chosen computer type and correctly display who wins/ties.
Write an algorithm for resolving a battle among the classic Pokemon types.
The user should select a type of either fire, water or grass. The computer should randomly select one of those three types as well.
To determine if the user or computer wins, or if it is a draw: fire is stronger than grass, grass is stronger than water, and water is stronger than fire. Whoever chose the stronger type is the winner. If both chose the same type it is a draw.
Answer:
# import random module import random print("\nWelcome the classic Pokemon Battle Game where you will test the " +"strength of your Pokemon against our Computer's Pokemon\n ") print("Winning Rules of the Pokemon Battle Game as follows: \n" + "Fire vs Grass->Fire wins \n" + "Grass vs Water->Grass wins \n" + "Water vs Fire->Water wins \n") print("Let the Battle BEGIN.........\n") while True: print("Enter the type of Pokemon: \n 1. Fire \n 2. Grass \n 3. Water \n") # take the input from user choice = int(input("User turn: ")) # looping until user enter invalid input while choice > 3 or choice < 1: choice = int(input("Invalid Type.....\nEnter a valid pokemon type: ")) # initialize value of choice_name variable # corresponding to the choice value if choice == 1: choice_name = 'Fire' elif choice == 2: choice_name = 'Grass' else: choice_name = 'Water' # print user choice print("User chose: " + choice_name + " type") print("\nNow its Computer's turn.......") # Computer chooses randomly any number # among 1 , 2 and 3. Using randint method # of random module comp_choice = random.randint(1, 3) # looping until comp_choice value # is equal to the choice value while comp_choice == choice: comp_choice = random.randint(1, 3) # initialize value of comp_choice_name # variable corresponding to the choice value if comp_choice == 1: comp_choice_name = 'Fire' elif comp_choice == 2: comp_choice_name = 'Grass' else: comp_choice_name = 'Water' print("Computer chose : " + comp_choice_name + " type") print(choice_name + " V/s " + comp_choice_name) print("\nLets's GO.......\n\n") # condition for winning if(choice==comp_choice): print("You both chose the same type...\n") result="Tie" elif ((choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 1)): print("Fire type wins.... ") result = "Fire" elif ((choice == 1 and comp_choice == 3) or (choice == 3 and comp_choice == 1)): print("Water type wins....") result = "Water" else: print("Grass type wins....") result = "Grass" # Printing either user or computer wins or if it's a tie if(result=="Tie"): print("<== After a fierce battle, the match is a draw ==>\n ") if result == choice_name: print("\n\n<== User wins ==>\n\n") else: print("\n\n<== Computer wins ==>\n\n") print("Do you want to play again? (Y/N)") ans = input() # if user input n or N then condition is True if ans == 'n' or ans == 'N': break # after coming out of the while loop # we print thanks for playing print("\nThanks for playing Pokemon Battle....\nUntil Next Time....\n")
Output:
Get Answers For Free
Most questions answered within 1 hours.