Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
pokemon/pokemonapp.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (58 sloc)
2.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import requests | |
#I have used a class for a better and more structured way to organise the code | |
class PokemonBattle: | |
def __init__(self): | |
self.my_pokemon = self.random_pokemon() | |
self.opponent_pokemon = self.random_pokemon() | |
def random_pokemon(self): | |
pokemon_number = random.randint(1, 151) | |
url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(pokemon_number) | |
response = requests.get(url) | |
pokemon = response.json() | |
return { | |
'name': pokemon['name'], | |
'id': pokemon['id'], | |
'height': pokemon['height'], | |
'weight': pokemon['weight'], | |
} | |
def run_battle(self): | |
print('You were given {}'.format(self.my_pokemon['name'])) | |
# Keeps track of the numbers of stats | |
stats_won = 0 | |
# Dictionary for the oponent's choice | |
opponent_choices = {'pokemon': self.opponent_pokemon['name']} | |
# Allow the player to choose values for all three stats using a for loop | |
for _ in range(3): | |
# Ask the user to choose a stat | |
stat_choice = input('Which stat do you want to use? (id, height, weight) ') | |
# Ask the user to enter the value for the chosen stat | |
my_stat = int(input('Enter the value for your chosen stat: ')) | |
# Store the opponent's choice for the current stat | |
opponent_stat_choice = random.choice(['id', 'height', 'weight']) | |
opponent_stat_value = self.opponent_pokemon[opponent_stat_choice] | |
opponent_choices[opponent_stat_choice] = opponent_stat_value | |
print('The opponent chose {} with a value of {}'.format(opponent_stat_choice, opponent_stat_value)) | |
# Get the opponent's stat value for the chosen stat | |
opponent_stat = int(opponent_stat_value) | |
# Compare the chosen stat values using if/else statements | |
if my_stat > opponent_stat: | |
print('You Win!') | |
stats_won += 1 | |
elif my_stat < opponent_stat: | |
print('You Lose!') | |
else: | |
print('Draw!') | |
# This game will be won only if one of the player wins at least twice(for two stats) | |
if stats_won >= 2: | |
print('Congratulations! You won the battle!') | |
else: | |
print('Sorry, you lost the battle.') | |
# Return the opponent's choices | |
return opponent_choices | |
# Create an instance of the PokemonBattle class | |
battle = PokemonBattle() | |
# Call the run_battle method | |
opponent_choices = battle.run_battle() | |
# Print the opponent's choices | |
print('Opponent\'s Choices:', opponent_choices) |