Skip to content
Snippets Groups Projects
Commit f0de4965 authored by Ashwin Maran's avatar Ashwin Maran
Browse files

add lab-p4 and p4

parent 2d45397e
No related branches found
No related tags found
No related merge requests found
# Lab-P4: Conditional Statements and Pokémon API
In P4, you will be playing with some Pokémon and you will simulate simple Pokémon battles using conditional statements. In Lab-P4, you will learn to use `project.py`, which you will need to complete P4. You will also be introduced to some simple conditional statements and 'helper functions' which will be useful for P4.
### Corrections/Clarifications
None yet
**Find any issues?** Report to us:
- Hakan Dingenc <dingenc@wisc.edu>
- Jodi Lawson <jlawson6@wisc.edu>
------------------------------
## Learning Objectives
In this lab, you will practice...
* Learning and using an 'API' (Application Programming Interface)
* Building 'helper' functions that can be used to create more advanced functions
* Writing conditions using if/elif/else statements
* Writing advanced conditions using nested if/else statements
* Writing advanced conditions using logical operators (or/and)
------------------------------
## Note on Academic Misconduct
You may do these lab exercises only with your project partner; you are not allowed to start working on Lab-P4 with one person, then do the project with a different partner. Now may be a good time to review [our course policies](https://cs220.cs.wisc.edu/s23/syllabus.html).
------------------------------
## Project partner
We strongly recommend students find a project partner. Pair programming is a great way to learn from a fellow student. Project difficulty increases exponentially in this course. Finding a project partner early on during the semester is a good idea.
If you are still looking for a project partner, take a moment now to ask around the room if anyone would like to partner with you on this project. Then you can work with them on this lab as well as the project.
------------------------------
## Segment 1: Setup
Create a `lab-p4` directory and download the following files into the `lab-p4` directory:
* `project.py`
* `pokemon_stats.csv`
* `type_effectiveness_stats.csv`
* `practice.ipynb`
* `practice_test.py`
**Note:** If you accidentally downloaded the file as a `.txt` (or `.cvs` or `.csv.txt`) instead of `.csv` (say `pokemon_stats.txt`), you can execute `mv pokemon_stats.txt pokemon_stats.csv` on a Terminal/PowerShell window. Recall that the `mv` (move) command lets you rename a source file (first argument in example: `pokemon_stats.txt`) to the destination file (second argument in example: `pokemon_stats.csv`).
Once you have downloaded the files, open a Terminal/PowerShell window and navigate to your `lab-p4` directory. Run `ls` to make sure the above files are available.
------------------------------
## Segment 2: Learning the API
You will be finishing the rest of your lab on `practice.ipynb`. Run the command `jupyter notebook` from your Terminal/PowerShell window. Remember not to close this Terminal/PowerShell window while Jupyter is running, and open a new Terminal/PowerShell window if necessary.
**Note:** For P4, you will be working on `p4.ipynb` which is very similar to `practice.ipynb`.
**Note:** Unlike `p4.ipynb`, you do **not** have to submit `practice.ipynb`. This notebook is solely for your practice.
------------------------------
You can now get started with [P4](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-s23-projects/-/tree/main/p4). **You can copy/paste and use any helper function that you have created here in P4.** Good luck and have fun!
# Images
Images from lab-p4 are stored here.
lab-p4/images/add_new_cell.PNG

6.97 KiB

lab-p4/images/pokemon_stats.png

35.7 KiB

lab-p4/images/type_effectiveness_stats.png

28.8 KiB

This diff is collapsed.
This diff is collapsed.
#!/usr/bin/python
import os, json, math
REL_TOL = 6e-04 # relative tolerance for floats
ABS_TOL = 15e-03 # absolute tolerance for floats
PASS = "PASS"
TEXT_FORMAT = "text" # question type when expected answer is a str, int, float, or bool
expected_json = {"1": (TEXT_FORMAT, 'Paldea'),
"2": (TEXT_FORMAT, 'Fire'),
"3": (TEXT_FORMAT, 'Dragon'),
"4": (TEXT_FORMAT, 106),
"5": (TEXT_FORMAT, 150),
"6": (TEXT_FORMAT, 150),
"7": (TEXT_FORMAT, 50),
"8": (TEXT_FORMAT, 140),
"9": (TEXT_FORMAT, 15),
"10": (TEXT_FORMAT, 2.0),
"11": (TEXT_FORMAT, 'Pikachu is from the Kanto region'),
"12": (TEXT_FORMAT, 'Snorlax'),
"13-1": (TEXT_FORMAT, 'Charmander'),
"13-2": (TEXT_FORMAT, 'Beedrill'),
"13-3": (TEXT_FORMAT, 'Draw'),
"14-1": (TEXT_FORMAT, 'Arcanine'),
"14-2": (TEXT_FORMAT, 'Draw'),
"14-3": (TEXT_FORMAT, 'Lugia'),
"15-1": (TEXT_FORMAT, 1),
"15-2": (TEXT_FORMAT, 2),
"16": (TEXT_FORMAT, True),
"17-1": (TEXT_FORMAT, True),
"17-2": (TEXT_FORMAT, True),
"18-1": (TEXT_FORMAT, True),
"18-2": (TEXT_FORMAT, False),
"18-3": (TEXT_FORMAT, False),
"18-4": (TEXT_FORMAT, False),
"18-5": (TEXT_FORMAT, True),
"19": (TEXT_FORMAT, 'Fire is stronger than Grass'),
"20-1": (TEXT_FORMAT, 'Charmander'),
"20-2": (TEXT_FORMAT, 'Draw'),
"20-3": (TEXT_FORMAT, 'Draw'),
"20-4": (TEXT_FORMAT, 'Skiploom'),
"20-5-1": (TEXT_FORMAT, 'Chimchar'),
"20-5-2": (TEXT_FORMAT, 'Draw')}
def check_cell(qnum, actual):
format, expected = expected_json[qnum[1:]]
try:
if format == TEXT_FORMAT:
return simple_compare(expected, actual)
else:
if expected != actual:
return "expected %s but found %s " % (repr(expected), repr(actual))
except:
if expected != actual:
return "expected %s" % (repr(expected))
return PASS
def simple_compare(expected, actual, complete_msg=True):
msg = PASS
if type(expected) == type:
if expected != actual:
if type(actual) == type:
msg = "expected %s but found %s" % (expected.__name__, actual.__name__)
else:
msg = "expected %s but found %s" % (expected.__name__, repr(actual))
elif type(expected) != type(actual) and not (type(expected) in [float, int] and type(actual) in [float, int]):
msg = "expected to find type %s but found type %s" % (type(expected).__name__, type(actual).__name__)
elif type(expected) == float:
if not math.isclose(actual, expected, rel_tol=REL_TOL, abs_tol=ABS_TOL):
msg = "expected %s" % (repr(expected))
if complete_msg:
msg = msg + " but found %s" % (repr(actual))
else:
if expected != actual:
msg = "expected %s" % (repr(expected))
if complete_msg:
msg = msg + " but found %s" % (repr(actual))
return msg
def check(qnum, actual):
msg = check_cell(qnum, actual)
if msg == PASS:
return True
print("<b style='color: red;'>ERROR:</b> " + msg)
__pokemon__= {}
__effectiveness__ = {}
def __init__():
"""This function loads the data from `pokemon_stats.csv` and `type_effectiveness_stats.csv`. This function runs automatically, when the module is imported"""
import csv
f = open('pokemon_stats.csv', encoding='utf-8')
raw_pkmn_data = list(csv.reader(f))
f.close()
pkmn_header = raw_pkmn_data[0]
pkmn_header.pop(0)
raw_pkmn_data = raw_pkmn_data[1:]
for pkmn_data in raw_pkmn_data:
pkmn_data.pop(0)
pkmn = {}
for i in range(len(pkmn_header)):
pkmn[pkmn_header[i]] = pkmn_data[i]
for stat in pkmn:
if stat in ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']:
pkmn[stat] = int(pkmn[stat])
__pokemon__[pkmn["Name"]] = pkmn
f = open('type_effectiveness_stats.csv', encoding='utf-8')
raw_type_data = list(csv.reader(f))
f.close()
type_header = raw_type_data[0]
raw_type_data = raw_type_data[1:]
for type1 in type_header[1:]:
__effectiveness__[type1] = {}
for row in raw_type_data:
type2 = row[0]
for i in range(1, len(row)):
type1 = type_header[i]
__effectiveness__[type1][type2] = float(row[i])
def print_stats(pkmn):
"""print_stats(pkmn) prints all the statistics of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
for stat in __pokemon__[pkmn]:
if not (stat == 'Type 2' and __pokemon__[pkmn][stat] == "DNE"):
print(stat, ": ", __pokemon__[pkmn][stat])
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_region(pkmn):
"""get_region(pkmn) returns the region of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Region']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_type1(pkmn):
"""get_type1(pkmn) returns Type 1 of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Type 1']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_type2(pkmn):
"""get_type2(pkmn) returns Type 2 of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Type 2']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_hp(pkmn):
"""get_hp(pkmn) returns the HP of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['HP']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_attack(pkmn):
"""get_attack(pkmn) returns the Attack of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Attack']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_defense(pkmn):
"""get_defense(pkmn) returns the Defense of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Defense']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_sp_atk(pkmn):
"""get_sp_atk(pkmn) returns the Special Attack of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Sp. Atk']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_sp_def(pkmn):
"""get_sp_def(pkmn) returns the Special Defense of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Sp. Def']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_speed(pkmn):
"""get_speed(pkmn) returns the Speed of the Pokémon with the name `pkmn`"""
if pkmn in __pokemon__:
return __pokemon__[pkmn]['Speed']
else:
raise Exception("Pokémon '" + pkmn + "' not found in the file")
def get_type_effectiveness(attacker_type, defender_type):
"""get_type_effectiveness(attacker_type, defender_type) returns the effectiveness of `attacker_type` attacks against `defender_type` Pokémon"""
if attacker_type in __effectiveness__ and defender_type in __effectiveness__[attacker_type]:
return __effectiveness__[attacker_type][defender_type]
elif attacker_type not in __effectiveness__:
if defender_type not in __effectiveness__:
raise Exception("Type '" + attacker_type + "' and Type '" + defender_type + "' not found in the file")
else:
raise Exception("Type '" + attacker_type + "' not found in the file")
else:
raise Exception("Type '" + defender_type + "' not found in the file")
__init__()
,Normal,Fire,Water,Electric,Grass,Ice,Fighting,Poison,Ground,Flying,Psychic,Bug,Rock,Ghost,Dragon,Dark,Steel,Fairy
Normal,1.0,1.0,1.0,1.0,1.0,1.0,2.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0
Fire,1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0,1.0,1.0,1.0,1.0,1.0,2.0,1.0,1.0,2.0
Dark,1.0,1.0,1.0,1.0,1.0,1.0,2.0,1.0,1.0,1.0,0.0,2.0,1.0,0.5,1.0,0.5,1.0,2.0
Steel,0.5,2.0,1.0,1.0,0.5,0.5,2.0,0.0,2.0,0.5,0.5,0.5,0.5,1.0,0.5,1.0,0.5,0.5
Fairy,1.0,1.0,1.0,1.0,1.0,1.0,0.5,2.0,1.0,1.0,1.0,0.5,1.0,1.0,0.0,0.5,2.0,1.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment