#๐Ÿ”’ Hook a duck help

92 messages ยท Page 1 of 1 (latest)

serene flower
#

i want it to replace a good duck with a bad duck everytime you hook a good duck. Basically the ratio of good duck to bad duck starts at 1 bad to 9 good ducks, and goes down to 2 bad, 8 good, 3 bad, 7 good etc..(after each good duck is hooked) I need the code to be basic because I'm just starting to learn Python. https://paste.pythondiscord.com/NF7Q

obsidian bobcatBOT
#

@serene flower

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

serene flower
#

can anyone help?

gilded spoke
#

There are a couple of ways that you could go about this

serene flower
#

oooooo

gilded spoke
#

have you learned about lists yet?

serene flower
#

i know a tini bit

gilded spoke
#

I would create list of the numbers 1 through 10. Then for each bad duck in that specific round, remove a random index from this list

serene flower
#

can you please right me an example?

gilded spoke
#
safe = []
bad_ducks = []
bad_ducks_count = 3

for num in range(1, 11):
  safe.append(num)

for time in range(bad_ducks):
  ind = random.randint(0, len(safe))
  removed = safe.pop(ind)
  bad_ducks.append(removed)

duck = 2
if duck in bad_ducks:
  print('you lost!')
else:
  print('you won!')
#

After the first for loop, safe would look like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

serene flower
gilded spoke
#

yea, but I'd recommend trying to learn what each part of this does

#

then each iteration of the second loop, it picks a random element of this safe list and removes it from the safe list

serene flower
#

k, what does the removed =8 safe.pop(ind)
bad_ducks.append(removed) do?

serene flower
gilded spoke
#

!e

x = [1, 2, 3]
removed = x.pop(1) # Removes 1st index (2) from x. Returns it
print(removed)
print(x)
obsidian bobcatBOT
gilded spoke
serene flower
#

๐Ÿ™‚

gilded spoke
#

append is a list method that adds a new element to the end of a list

#

!e

x = []
x.append(1)
print(x)
obsidian bobcatBOT
serene flower
gilded spoke
#

Instead of trying to copy and paste the entire thing into the code, it'd be best to try to see what each thing does

#

then insert it accordingly

#
import time
#duck game
from random import randint
print ("hook a duck")
feeling_brave = True
score = 0


#the main loop
while feeling_brave:
    bad_duck = randint(1, 10)
    time.sleep(1)
    print ("make sure you pick the right one")
    time.sleep(1)
    print ("which duck do you choose?")
    duck = input('1, 2, 3, 4, 5, 6, 7, 8, 9 or 10')
    duck_num = int(duck)
    if duck_num == bad_duck:
        time.sleep(3)
        print("bad duck!")
        time.sleep(1)
        print("you lost!")
        time.sleep(1)
        print("Game over!")
        print('you scored', score)
        time.sleep(10)
        feeling_brave = false

        

    
    else:
        time.sleep(3)
        print("safe duck")
        time.sleep(1)
        print("you ready hook a duck stick")
        score = score + 1
        
        time.sleep(2)
#

programs are usually able to be separated into setup, computation, and result steps

#

your current setup is primarily just the bad_duck = randint(1, 10)

#

that's what you're using to find out what the bad_ducks are

#

part of my code is to determine what the bad_ducks are

serene flower
#

alr, i have changed my code to that, i will test it now ๐Ÿ™‚

gilded spoke
#

mind I see what you've changed?

serene flower
#

!e import time
from random import randint

Initial setup for the duck game

print("Hook a duck")
feeling_brave = True
score = 0

Main game loop

while feeling_brave:
bad_duck = randint(1, num_ducks) # Randomly assign a "bomb duck" within the range of ducks
time.sleep(1)
print("Make sure you pick the right one!")
time.sleep(1)
print("Which duck do you choose?")
duck = input('1, 2, 3, 4, 5, 6, 7, 8, 9 or 10')

duck_num = int(duck)

# Check if the chosen duck is a bomb duck
if duck_num == bad_duck:
    time.sleep(3)
    print("Bad duck!")
    time.sleep(1)
    print("You lost!")
    time.sleep(1)
    print("Game over!")
    print(f'You scored: {score}')
    time.sleep(10)
    feeling_brave = False  # End the game

else:
    # Player chose a safe duck
    time.sleep(3)
    print("Safe duck!")
    score += 1  # Increase score

    
    time.sleep(2)
    print(f"Your score is now {score}. Get ready for the next round!")
obsidian bobcatBOT
serene flower
#

it comes out with an error ๐Ÿ˜ฆ

gilded spoke
#

one of the biggest skills in programming is reading the error messages as they give insight as to what went wrong

serene flower
#

was it this that whent wrong? NameError: name 'num_ducks' is not defined

gilded spoke
#

yea

#

that was the error, and it says where it happened

serene flower
#

line 12

gilded spoke
#

That error means that you're trying to use a variable that hasn't been given a value yet

#

the num_ducks variable

#

that I used in my code as an example

serene flower
#

yup

gilded spoke
#

I just realized that the only thing you changed was putting 'num_ducks' in the place of 10 in the randint

serene flower
#

i mean i also changed the time.sleep()

gilded spoke
#

That wouldn't change anything when it comes to the output

#

since that just makes the program wait

#

I don't think you can do this without lists

serene flower
#

could you teach me lists?

gilded spoke
#

!resources

obsidian bobcatBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

serene flower
#

what is the command for testing code?

gilded spoke
#

wdym?

serene flower
#

on the discord

gilded spoke
#

it's !e

#

when pasting code, please paste it like this
```py
# Code here
```
so it looks like this

# Code here
#

so something like this
!e

print('hello world')
serene flower
#

!e py !e mylist = ['apple', 'milk', 'cheese', 'icecream',] mylist[1] = 'cake' print(mylist)

gilded spoke
#

those are backticks, not quotes

serene flower
#

oh found em

gilded spoke
#

those are not backticks

obsidian bobcatBOT
serene flower
#

hmm

gilded spoke
#

!e ```py
print('hello world')
```

#

it should look like this

serene flower
#

!epy mylist = ['apple', 'milk', 'cheese', 'icecream'] mylist[1] = 'cake' print(mylist[1]

gilded spoke
#

no space after the ``` and py

#

and the py should be on the same line as ```

obsidian bobcatBOT
serene flower
#

!epy mylist = ['apple', 'milk', 'cheese', 'icecream'] mylist[1] = 'cake' print(mylist[1]

obsidian bobcatBOT
gilded spoke
#

bruh

serene flower
#

this is so confusing

gilded spoke
#

!e ```py
print('hello world')

obsidian bobcatBOT
serene flower
#

!e```py
mylist = ['apple', 'milk','cheese','icecream']
mylist[1] = 'cake'
print("list", mylist)

obsidian bobcatBOT
serene flower
#

yahoo it worked

#

so is that what we would do?

gilded spoke
#

I'd recommend getting familiar with lists

#

before continuing

#

search up a python list tutorial and follow what they do in that video or site

#

then do some playign around on your own

serene flower
#

ok tysm

serene flower
#

i have made this

#

were would i put this in my code?

obsidian bobcatBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.