Question

Python Code Please Write an algorithm for resolving a battle among the classic Pokemon types.       ...

Python Code Please

  1. 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. Whomever chose the stronger type is the winner. If both chose the same type it is a draw.

Homework Answers

Answer #1

ANSWER:

For computer choice, we make a list of all the choices and use random.choice(list), this selects one of the item in the list, then using simple else-if statements, we decide which player is winner,

using while loop to continuously repeat the whole code, so that after 1 player wins, again new game is started on its own , and when user inputs anything other than the valid items present in the choice list, then game is over, code is exited.

It is is very simple program, if you have basics of python while , if-else clear, once you go through the below code, you will be more clear about how the code is working, I have included comments to give you better understanding of the code:

CODE:

import random   # importing random module

while True:
    user_input = input("Enter any one choice from fire/water/grass :")
    user_input = user_input.lower()     # converting the user input to lower case to ease the comparision
    choice_list = ['fire','water','grass']   # a list of choices to pick randomly
    computer_choice = random.choice(choice_list) # using random.choice() to choose among the three options
    if user_input in choice_list:        # checking if the user input is either water grass or fire and nothing else
        # player having choice with greater power wins
        if user_input == 'fire' and computer_choice == 'grass':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("user wins")
        elif computer_choice == 'fire' and user_input == 'grass':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("computer wins")
        elif user_input == 'grass' and computer_choice == 'water':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("user wins")
        elif computer_choice == 'grass' and user_input == 'water':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("computer wins")
        elif user_input == 'water' and computer_choice == 'fire':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("user wins")
        elif computer_choice == 'water' and user_input == 'fire':
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("computer wins")
        elif computer_choice == user_input:     # choice of both player is same so draw
            print("User -->" + user_input + "   " + "computer -->" + computer_choice)
            print("draw")
    else:       # if choice not in choice_list, game over
        print("invalid input")
        print("Exiting the Game")
        exit()

OUTPUT:

you can see the output , it shows the choice of both the player and the computer and then checks which choice is powerful, then declares the winner.

for unknown input , the game ends.

THANK YOU....!!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks! The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT