Coding a simple fishing game, logic being ignored (EDITED)

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. :grinning:

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())

Hi Nick @stoochie !

It looks like you tried to do the right thing to format your code when you pasted it into the post, but it didn’t quite work out. Try like this:

  1. Edit your post and delete all the code that is there
  2. Put the cursor on a new blank line
  3. Look in the editing toolbar above the editing area, for an option called “Preformatted Text”. It may be under the “gear” shaped menu at the right end of the toolbar. This should insert a code block placeholder (see below)
  4. Switch to your script file, select all of it (Control+a), and copy
  5. Switch back to the post editor, delete anything inside the code block, and paste your script in.

Check the preview and it should all be nicely indented and highlighted like it would be in your code editor.

When you select the “Preformatted Text” option on an empty line, it should insert something that looks like this:

```
type or paste code here
```

The character you’re looking for is called a “back tick” or “grave accent” and is located to the left of the “1”/“!” key on a US ANSI keyboard but may be located elsewhere on different keyboard layouts.

1 Like

thankyou Matt, I have fixed it up :slight_smile:

You are missing a comma between the first two strings.
Without the comma python treats the first entry in list as “ SpecialPirate Flag”.

Also you have two copies of list.
Suggest you define one copy of the list.

FISH_NAMES = ["Special", "Pirate Flag", "Plastic Bag", …

I would not use Fish as the loop variables as its the name of the class.

In your own words: why do you expect that a random.choice from fishies should ever be in esky?

What part of the code causes esky to contain any Fish? Exactly which Fish do you think it should contain, and why? What does it mean to check whether a given Fish is in esky? What logic do you expect to be used for this task, and why?

Okay. You know that len(esky)>10 does what it’s supposed to. What part of your code makes that happen? You know that for fish in esky.fish: works. Did you try for fish in esky:? Would that work? What happens if you try that? Does that help you understand the problem?

If the above is not enough information (i.e. you don’t just have a simple oversight in the code, and also need to understand the corresponding technique), please see: