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
#๐ Hook a duck help
92 messages ยท Page 1 of 1 (latest)
@serene flower
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.
Closes after a period of inactivity, or when you send !close.
can anyone help?
There are a couple of ways that you could go about this
oooooo
have you learned about lists yet?
i know a tini bit
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
can you please right me an example?
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]
so could i put that code in then adapt it to the rest of my code?
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
k, what does the removed =8 safe.pop(ind)
bad_ducks.append(removed) do?
so could you find a safe duck and just keep choosing it?
pop is a list method that removes an index from a list and returns it
!e
x = [1, 2, 3]
removed = x.pop(1) # Removes 1st index (2) from x. Returns it
print(removed)
print(x)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 2
002 | [1, 3]
no. The safe and bad ducks change each round
๐
append is a list method that adds a new element to the end of a list
!e
x = []
x.append(1)
print(x)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1]
were in my script would this go?
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
alr, i have changed my code to that, i will test it now ๐
mind I see what you've changed?
!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!")
:x: Your 3.12 eval job has completed with return code 1.
001 | Hook a duck
002 | Traceback (most recent call last):
003 | File "/home/main.py", line 12, in <module>
004 | bad_duck = randint(1, num_ducks) # Randomly assign a "bomb duck" within the range of ducks
005 | ^^^^^^^^^
006 | NameError: name 'num_ducks' is not defined
it comes out with an error ๐ฆ
one of the biggest skills in programming is reading the error messages as they give insight as to what went wrong
was it this that whent wrong? NameError: name 'num_ducks' is not defined
line 12
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
yup
I just realized that the only thing you changed was putting 'num_ducks' in the place of 10 in the randint
i mean i also changed the time.sleep()
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
could you teach me lists?
!resources
Resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
what is the command for testing code?
wdym?
on the discord
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')
!e py !e mylist = ['apple', 'milk', 'cheese', 'icecream',] mylist[1] = 'cake' print(mylist)
those are backticks, not quotes
oh found em
those are not backticks
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
hmm
!epy mylist = ['apple', 'milk', 'cheese', 'icecream'] mylist[1] = 'cake' print(mylist[1]
:x: Your 3.12 eval job has completed with return code 1.
001 | File "/home/main.py", line 1
002 | py !e mylist = ['apple', 'milk', 'cheese', 'icecream',]
003 | ^
004 | SyntaxError: invalid syntax
!epy mylist = ['apple', 'milk', 'cheese', 'icecream'] mylist[1] = 'cake' print(mylist[1]
:x: Your 3.12 eval job has completed with return code 1.
001 | File "/home/main.py", line 1
002 | py mylist = ['apple', 'milk', 'cheese', 'icecream']
003 | ^^^^^^
004 | SyntaxError: invalid syntax
bruh
this is so confusing
!e ```py
print('hello world')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
hello world
!e```py
mylist = ['apple', 'milk','cheese','icecream']
mylist[1] = 'cake'
print("list", mylist)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
list ['apple', 'cake', 'cheese', 'icecream']
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
ok tysm
!e```py
mylist = ['good', 'good','good','good','good','good','good','good','good','good']
mylist[score] = 'bad'
i have made this
were would i put this in my code?
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.