Skip to content

Commit 2b6a0a8

Browse files
authored
Merge pull request #6 from Rahullkumr/addition
Add: USA states guessing game
2 parents 1bf4b09 + 89fd8c5 commit 2b6a0a8

13 files changed

Lines changed: 3365 additions & 2 deletions

Day25-Pandas-USAstatesGame/2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv

Lines changed: 3024 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
state,x,y
2+
Alabama,139,-77
3+
Alaska,-204,-170
4+
Arizona,-203,-40
5+
Arkansas,57,-53
6+
California,-297,13
7+
Colorado,-112,20
8+
Connecticut,297,96
9+
Delaware,275,42
10+
Florida,220,-145
11+
Georgia,182,-75
12+
Hawaii,-317,-143
13+
Idaho,-216,122
14+
Illinois,95,37
15+
Indiana,133,39
16+
Iowa,38,65
17+
Kansas,-17,5
18+
Kentucky,149,1
19+
Louisiana,59,-114
20+
Maine,319,164
21+
Maryland,288,27
22+
Massachusetts,312,112
23+
Michigan,148,101
24+
Minnesota,23,135
25+
Mississippi,94,-78
26+
Missouri,49,6
27+
Montana,-141,150
28+
Nebraska,-61,66
29+
Nevada,-257,56
30+
New Hampshire,302,127
31+
New Jersey,282,65
32+
New Mexico,-128,-43
33+
New York,236,104
34+
North Carolina,239,-22
35+
North Dakota,-44,158
36+
Ohio,176,52
37+
Oklahoma,-8,-41
38+
Oregon,-278,138
39+
Pennsylvania,238,72
40+
Rhode Island,318,94
41+
South Carolina,218,-51
42+
South Dakota,-44,109
43+
Tennessee,131,-34
44+
Texas,-38,-106
45+
Utah,-189,34
46+
Vermont,282,154
47+
Virginia,234,12
48+
Washington,-257,193
49+
West Virginia,200,20
50+
Wisconsin,83,113
51+
Wyoming,-134,90
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# USA States Game
2+
3+
This is an interactive Python game where users attempt to name all 50 U.S. states. The program will show an image of the United States and ask users to input state names. If a correct state name is entered, it will be displayed on the map. The game also generates a file with the names of any states the player missed.
4+
5+
![](./USAstatesGame.png)
6+
7+
## Features
8+
- Displays a map of the USA with a blank outline of the states.
9+
- Allows users to guess states' names and shows correct guesses on the map.
10+
- Generates a `states_you_missed.csv` file listing any states not guessed during the game.
11+
12+
## Prerequisites
13+
1. **Python 3.x**: Ensure Python is installed. You can download it from [Python.org](https://www.python.org/).
14+
2. **Pandas**: Install the `pandas` library for data manipulation:
15+
```
16+
pip install pandas
17+
```
18+
3. **Turtle Graphics**: The `turtle` module is a standard Python library, so no additional installation is needed.
19+
20+
## Project Structure
21+
- `main.py`: Main game file containing the code to run the game.
22+
- `50_states.csv`: CSV file with state names and their coordinates.
23+
- `blank_states_img.gif`: Image of the USA map outline used as the game background.
24+
25+
## Getting Started
26+
27+
1. **Clone the repository**: Clone the project to your local machine using Git.
28+
```
29+
git clone https://github.com/Rahullkumr/100daysofpython.git
30+
```
31+
32+
2. **Navigate to the project directory**:
33+
```
34+
cd Day25-Pandas-USAstatesGame
35+
```
36+
37+
3. **Ensure all required files are in the directory**:
38+
- `main.py`
39+
- `50_states.csv`
40+
- `blank_states_img.gif`
41+
42+
## Running the Game
43+
44+
To start the game, execute the following command:
45+
```
46+
python main.py
47+
```
48+
49+
## How to Play
50+
51+
1. When the game starts, a prompt will appear asking for a state name.
52+
2. Type the name of a state and press Enter.
53+
3. If the state name is correct, it will appear on the map at the state's location.
54+
4. Type "Exit" if you want to stop the game early.
55+
- When you exit, a `states_you_missed.csv` file will be created in the project directory, listing any states you did not guess.
56+
57+
## Note
58+
59+
- Ensure that `blank_states_img.gif` is present in the same directory as the code. If it's missing, the game will not display the background map.
60+
61+
## Author
62+
- Created by [Rahul Kumar](https://github.com/rahullkumr)
63+
64+
Enjoy learning and challenging yourself with this USA states guessing game!
184 KB
Loading
40.2 KB
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
""" BASICS OF DATA ANALYSIS """
2+
3+
4+
# with open('./weather_data.csv', 'r') as file:
5+
# data = file.readlines()
6+
# print(data)
7+
"""There is a lot of headache in cleaning the above data, so try other way (see below)"""
8+
9+
10+
# import csv
11+
# with open('./weather_data.csv') as data_file:
12+
# data = csv.reader(data_file)
13+
# temperatures = []
14+
# for row in data:
15+
# if row[1] != 'temp':
16+
# temperatures.append(int(row[1]))
17+
#
18+
# print(temperatures)
19+
"""
20+
A lot of lines of code is written just to get one column, what if there are 100s of columns and complex data,
21+
so it is not a convenient way. Now let's use PANDAS library and see the magic.
22+
"""
23+
24+
25+
import pandas as pd
26+
data = pd.read_csv('./weather_data.csv')
27+
print(data['temp'])
28+
29+
# DataFrame: whole table of Excel sheet or google sheet (2D)
30+
# eg: data
31+
# Series: a column of Excel sheet or google sheet (1D)
32+
# eg: data['temp']
33+
34+
data_as_dict = data.to_dict()
35+
temp_list = data['temp'].to_list()
36+
print(data_as_dict)
37+
print(temp_list)
38+
39+
40+
print('Average Temperature: ', sum(temp_list)/len(temp_list))
41+
print('Average temp using series: ', data['temp'].mean())
42+
print('Max temp using series: ', data['temp'].max()) # data['temp'] == data.temp
43+
print('Max temp using series: ', data.temp.max())
44+
print(data.condition)
45+
46+
47+
# Get data in row
48+
print(data[data.day == 'Monday']) # its dataframe
49+
50+
# Get the row which has maximum temperature
51+
print(data[data.temp == data.temp.max()]) # its dataframe
52+
53+
# Get Monday's condition
54+
print(data[data.day == "Monday"].condition)
55+
56+
# challenge: show Monday's temperature in Fahrenheit ((0°C × 9/5) + 32 = 32°F)
57+
monday_temp = data.temp[0]
58+
print(monday_temp)
59+
print("Monday's temp in Fahrenheit: ", monday_temp * 9/5 + 32, "°F")
60+
61+
62+
# create a DataFrame from scratch
63+
data_dict = {
64+
"names": ['Amit', 'Rohit', 'Sumit'],
65+
"score": [45, 73, 66]
66+
}
67+
df_from_scratch = pd.DataFrame(data_dict)
68+
print(df_from_scratch)
69+
70+
71+
# save the DataFrame in csv format
72+
df_from_scratch.to_csv('./students_data.csv') # path where you want to save

Day25-Pandas-USAstatesGame/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import turtle
2+
import pandas as pd
3+
4+
screen = turtle.Screen()
5+
screen.title('USA states game')
6+
image = 'blank_states_img.gif'
7+
screen.addshape(image)
8+
turtle.shape(image)
9+
10+
data = pd.read_csv('./50_states.csv')
11+
all_states = data.state.to_list()
12+
guessed_states = []
13+
14+
while len(guessed_states) < 50:
15+
ans_state = (turtle.textinput(title=f'{len(guessed_states)}/50 States Correct',
16+
prompt="What's another state's name? ")).title()
17+
18+
if ans_state == 'Exit':
19+
# states_you_missed.csv
20+
missed_states_dict = {
21+
"Missed States": [state for state in all_states if state not in guessed_states]
22+
}
23+
df = pd.DataFrame(missed_states_dict)
24+
df.to_csv('./states_you_missed.csv', mode='w')
25+
break
26+
27+
if ans_state in all_states:
28+
guessed_states.append(ans_state)
29+
t = turtle.Turtle()
30+
t.hideturtle()
31+
t.penup()
32+
state_data = data[data['state'] == ans_state]
33+
t.goto(state_data.x.item(), state_data.y.item())
34+
t.write(ans_state)
35+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Importing the pandas library, commonly used for data manipulation and analysis
2+
import pandas as pd
3+
4+
# Reading the CSV file into a DataFrame
5+
data = pd.read_csv('./2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv')
6+
7+
8+
'''
9+
# ============== This is my way ===========================
10+
11+
pfc = data['Primary Fur Color']
12+
fur_color_dict = {
13+
"Fur Color": ['Gray', 'Cinnamon', 'Black'],
14+
"Count": []
15+
}
16+
g_count, b_count, c_count = 0, 0, 0
17+
for i in pfc:
18+
if i in ('Gray', 'Cinnamon', 'Black'):
19+
g_count += 1 if i == "Gray" else 0
20+
c_count += 1 if i == "Cinnamon" else 0
21+
b_count += 1 if i == "Black" else 0
22+
fur_color_dict["Count"] = [g_count, c_count, b_count]
23+
print(fur_color_dict)
24+
'''
25+
26+
# Better way
27+
pfc = data['Primary Fur Color']
28+
grey_squirrel_count = len(data[data['Primary Fur Color'] == 'Gray'])
29+
cinnamon_squirrel_count = len(data[data['Primary Fur Color'] == 'Cinnamon'])
30+
black_squirrel_count = len(data[data['Primary Fur Color'] == 'Black'])
31+
32+
fur_color_dict = {
33+
"Fur Color": ['Gray', 'Cinnamon', 'Black'],
34+
"Count": [grey_squirrel_count, cinnamon_squirrel_count, black_squirrel_count]
35+
}
36+
37+
# Creating and printing the DataFrame
38+
df = pd.DataFrame(fur_color_dict)
39+
print(df)
40+
41+
# Saving the DataFrame as a new CSV file with squirrel fur color counts
42+
df.to_csv('./squirrel_count.csv')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
,Fur Color,Count
2+
0,Gray,2473
3+
1,Cinnamon,392
4+
2,Black,103
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
,Missed States
2+
0,Arizona
3+
1,Arkansas
4+
2,Colorado
5+
3,Connecticut
6+
4,Delaware
7+
5,Florida
8+
6,Georgia
9+
7,Hawaii
10+
8,Idaho
11+
9,Illinois
12+
10,Indiana
13+
11,Iowa
14+
12,Kansas
15+
13,Kentucky
16+
14,Louisiana
17+
15,Maine
18+
16,Maryland
19+
17,Massachusetts
20+
18,Michigan
21+
19,Minnesota
22+
20,Mississippi
23+
21,Missouri
24+
22,Montana
25+
23,Nebraska
26+
24,Nevada
27+
25,New Hampshire
28+
26,New Jersey
29+
27,New Mexico
30+
28,New York
31+
29,North Carolina
32+
30,North Dakota
33+
31,Oklahoma
34+
32,Oregon
35+
33,Pennsylvania
36+
34,Rhode Island
37+
35,South Carolina
38+
36,South Dakota
39+
37,Tennessee
40+
38,Texas
41+
39,Utah
42+
40,Vermont
43+
41,Virginia
44+
42,West Virginia
45+
43,Wisconsin
46+
44,Wyoming

0 commit comments

Comments
 (0)