Hi friends, your help would be much appreciated with this one
sorry in advance if i am not asking for help in the right way or have not formatted this document correctly, its my first time asking for help, please bear with me.
the âif statementâ with asterixs ** before it is being ignored (it is the first staement inside the still fishing while loop)
I have tried re arranging the order of the if statements
my code does not realise that I have added a catch to my esky.
it will not ask me if I would like to throw my catch out
it will always perform one of the other 2 if statements in the while loop
these 3 statements all work perfectly so i know the catch is actually being added
if len(esky)>10:
for fish in esky.fish
if esky.score()
here is my code ::
import random
from collections import Counter
import hashlib
from passlib.hash import argon2
import json
from pydantic import BaseModel
import csv
class Fish(BaseModel):
name: str
score: int
message: str
class Esky(BaseModel):
fish: list[Fish]
def score(self):
return sum(Fish.score for Fish in self.fish)
def add(self, catch):
self.fish.append(catch)
def __len__(self):
return len(self.fish)
def counter(self):
counts = Counter(Fish.name for Fish in self.fish)
return print(counts)
fishies = []
if input('Load from File: ') == 'y':
with open("test.json") as f:
esky = Esky.model_validate_json(f.read())
else:
esky = Esky(fish=[])
with open('fish.csv', newline='') as csvfile:
FISHCSV = csv.reader(csvfile, delimiter=',')
for row in FISHCSV:
newfish = Fish(name=row[0], score=int(row[1]), message=row[2])
fishies.append(newfish)
still_fishing = True
while still_fishing:
catch = random.choice(fishies)
*if catch in esky:*
* k = ""*
* while k == "":*
* k = input(*
* f"You've caught {catch.name}, you already have a {catch.name} in your esky, would you like to release this one? please enter y/n")*
* if k == "y":*
* break*
* elif k == "n":*
* esky.add(catch)*
* break*
* else:*
* k = ""*
if catch not in esky and catch.name in ["Special" "Pirate Flag", "Plastic Bag", "Shoe", "Bottle", "Seaweed", "Piece of Atlantis"]:
k = ""
while k == "":
k = input(
f"You've caught {catch.name}, it seems pretty useless, would you like to throw it away? please enter y/n")
if k == "y":
break
elif k == "n":
esky.add(catch)
break
else:
k = ""
if catch not in esky and catch.name not in ["Special" "Pirate Flag", "Plastic Bag", "Shoe", "Bottle", "Seaweed", "Piece of Atlantis"]:
print(catch.message)
esky.add(catch)
if len(esky) > 10:
k = ""
while k == "":
k = input(
"Your esky is quite full, would you like to keep fishing? y/n")
if k == "y":
continue
elif k == "n":
still_fishing = False
break
else:
k = ""
for fish in esky.fish:
print(f"Your esky contains {fish.name}")
if esky.score() > 50: print(f"you won with a score of {esky.score()}")
if esky.score() < 50: print(f"you lost with a score of {esky.score()}")
e = esky
with open("test.json", "wb") as f:
f.write(e.model_dump_json().encode())