This commit is contained in:
winneratwin 2022-03-02 21:44:58 +00:00
parent f2beb08511
commit 8145c00893
Signed by: winneratwin
GPG Key ID: CDBC42F8803D689E
11 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Download and run - don't worry for the moment about how it works. You will add code to it later.
files:
game.py

9
week 7/Names.csv Normal file
View File

@ -0,0 +1,9 @@
Name,Country
Ben,England
Azam,Iran
Ibrahim,Palestine
Chrissie,Scotland
Ken,Scotland
Kathy,Scotland
Nelson,Nigeria
Jane,New Zealand
1 Name Country
2 Ben England
3 Azam Iran
4 Ibrahim Palestine
5 Chrissie Scotland
6 Ken Scotland
7 Kathy Scotland
8 Nelson Nigeria
9 Jane New Zealand

View File

@ -0,0 +1,3 @@
files:
Python_csv.pptx
Python_dict_csv.pptx

BIN
week 7/Python_csv.pptx Normal file

Binary file not shown.

BIN
week 7/Python_dict_csv.pptx Normal file

Binary file not shown.

1
week 7/Quiz.md Normal file
View File

@ -0,0 +1 @@
https://realpython.com/quizzes/python-csv/viewer/

BIN
week 7/Spreadsheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -0,0 +1,7 @@
Today's tasks include Python Dictionary, Python Libraries and reading CSV files.
files:
csvreading.py
foods.py
Names.csv

22
week 7/csvreading.py Normal file
View File

@ -0,0 +1,22 @@
import csv
#import the csv ibrary for working with csv files
import random
from datetime import date
today = date.today()
print (today)
with open('Names.csv') as csvfile:
favourites = csv.reader(csvfile, delimiter=',')
for row in favourites:
print(row)
name = "Jane"
country = "New Zealand"
with open('Names.csv', mode="a") as csvfile:
favourites = csv.writer(csvfile, delimiter=',')
favourites.writerow ([name, country])

12
week 7/foods.py Normal file
View File

@ -0,0 +1,12 @@
foods = {"Ben": "Bacon Rolls","Sarah":"cheese", "Kenny":"Bacon Rolls", "Ibrahim": "Dates", "Azam": "Cake"}
print (foods)
print (foods["Azam"])
foods ["Chrissie"] = "Pasta"
print (foods)
#dot operator calls a FUNCTION
foods.update({"Sarah": "Steak"})
#round brackets indicate a FUNCTION ARGUMENT
print (foods)
#del is a KEYWORD
del foods ["Ibrahim"]
print (foods)

117
week 7/game.py Normal file
View File

@ -0,0 +1,117 @@
def showInstructions():
# Print a main menu and the commands
print('''
RPG Game
========
Get to the Garden with a key and a potion.
Avoid the monsters!
You are getting tired; each time you move you lose 1 health point.
Commands:
go [direction]
get [item]
''')
def showStatus():
# Print the player's current status
print('---------------------------')
print(name + ' is in the ' + currentRoom)
print("Health : " + str(health))
# Print the current inventory
print("Inventory : " + str(inventory))
# Print an item if there is one
if "item" in rooms[currentRoom]:
print('You see a ' + rooms[currentRoom]['item'])
print("---------------------------")
# Set up the game
name = None
health = 5
currentRoom = 'Hall'
inventory = []
#-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
# Load data from the file
# A dictionary linking a room to other room positions
rooms = {
'Hall' : { 'south' : 'Kitchen',
'east' : 'Dining Room',
'item' : 'key'
},
'Kitchen' : { 'north' : 'Hall',
'item' : 'monster'
},
'Dining Room' : { 'west' : 'Hall',
'south' : 'Garden',
'item' : 'potion'
},
'Garden' : { 'north' : 'Dining Room' }
}
# Ask the player their name
if name is None:
name = input("What is your name, Adventurer? ")
showInstructions()
# Loop forever
while True:
showStatus()
# Get the player's next 'move'
# .split() breaks it up into an list array
# e.g. typing 'go east' would give the list:
# ['go','east']
move = ''
while move == '':
move = input('>')
move = move.lower().split()
# If they type 'go' first
if move[0] == 'go':
health = health - 1
# Check that they are allowed wherever they want to go
if move[1] in rooms[currentRoom]:
# Set the current room to the new room
currentRoom = rooms[currentRoom][move[1]]
# or, if there is no door (link) to the new room
else:
print('You can\'t go that way!')
# If they type 'get' first
if move[0] == 'get' :
# If the room contains an item, and the item is the one they want to get
if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
# Add the item to their inventory
inventory += [move[1]]
# Display a helpful message
print(move[1] + ' got!')
# Delete the item from the room
del rooms[currentRoom]['item']
# Otherwise, if the item isn't there to get
else:
# Tell them they can't get it
print('Can\'t get ' + move[1] + '!')
# Player loses if they enter a room with a monster
if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
print('A monster has got you... GAME OVER!')
break
if health == 0:
print('You collapse from exhaustion... GAME OVER!')
# Player wins if they get to the garden with a key and a potion
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
print('You escaped the house... YOU WIN!')
break
#-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
# Save game data to the file