Python Code Please
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.
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....!!!!
Get Answers For Free
Most questions answered within 1 hours.