#๐ Python assignment help
86 messages ยท Page 1 of 1 (latest)
@eternal knot
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.
Need help with homework assignment
it says code is too long
Read the bot message
play = [[0, 0, 0]] * 7
This is a problem
You'll have the same list seven times. So changes to one "row" will appear in all rows
Because they're the same list
what should i do about that?
i was provided a starter file and this was already included in that starter file
Who made the starter file?
I would see if you can disenroll and get your money back
In the meantime, you'd use a list comprehension to make sure that each sub-list is a separate object
!listcomp
Do you ever find yourself writing something like this?
>>> squares = []
>>> for n in range(5):
... squares.append(n ** 2)
[0, 1, 4, 9, 16]
Using list comprehensions can make this both shorter and more readable. As a list comprehension, the same code would look like this:
>>> [n ** 2 for n in range(5)]
[0, 1, 4, 9, 16]
List comprehensions also get an if clause:
>>> [n ** 2 for n in range(5) if n % 2 == 0]
[0, 4, 16]
For more info, see this pythonforbeginners.com post.
so i would use listcomp with play = [[0, 0, 0]] * 7
The list comprehension would replace that.
is this better?
# Declare a 7 row by 3 column list
column = 3
row = 7
play = [[int() for _ in range(column)] for _ in range(row)]
or this for more condensed play = [[int() for _ in range(3)] for _ in range(7)]
!code
play = [[int() for in range(3)] for in range(7)]
I see that you copy-pasted by highlighting your text ๐
some characters have special meaning for formatting in Markdown, which discord employs
italics for example
that word is surrounded by *
I'd personally write 0 instead of int(). And I'd probably not use a list comprehension for the 3-element inner list. But ya.
you can right click and copy text option if you need to copy the raw text of a message
ok
Ya, you can't mutate a number, so it doesn't matter if you have multiple references to the same number.
this is a program for the 21 card trick do i want to the values in list to be immutable
Numbers are just inherently immutable. If you want a list of numbers, the mutability isn't your main concern. The fact that they're immutable makes them "safer" in some circumstances though.
ok
so i changed the line deal(deck) to deal(deck,play) and that fix one error i was having
i can finally see the 3 columns now but it's all the same card atm
https://paste.pythondiscord.com/ARRQ so i think i got most of it working now
just not printing the cards right
https://imgur.com/a/SvXzNv4
What's wrong?
each card is supposed to be a different card
then the program is supposed to pick up the cards and then use those same cards and put them in each column
Notice how BuildDeck doesn't use the deck parameter. That function does some work then throws away everything it does.
So you create deck via deck = [0] * 52, then it doesn't change after that.
Then that deck full of only 0s is passed into Deal.
i see what you mean
i was trying to make BuildDeck
build a deck of 52 cards in random order but for some reason it's not working
You want a list full of unique numbers in random order?
yeah from 1 to 52
Just use random.shuffle.
or 0 or 51
!e
import random
cards = list(range(52))
random.shuffle(cards)
print(cards)
@cursive mica :white_check_mark: Your 3.12 eval job has completed with return code 0.
[36, 10, 21, 6, 41, 22, 48, 18, 29, 16, 9, 11, 42, 38, 17, 12, 2, 37, 28, 7, 8, 39, 3, 31, 51, 34, 23, 44, 32, 27, 4, 30, 0, 13, 35, 20, 24, 47, 50, 1, 45, 25, 43, 15, 49, 33, 26, 40, 5, 19, 14, 46]
That's 0-51 inclusive.
would i use deck = list(range(52))?
Well, that's a very easy to create a list with the numbers 0-51. That's what'd I'd use unless they're forcing you to do something different.
i think it'll work as long as put it into the BuildDeck function
That won't work with how your code currently is, where deck is passed in. You'd need to return the list of numbers from BuildDeck.
Wow, there's not a single return in that code. Have you learned about return?
i don't remember how it works tho
!return
A value created inside a function can't be used outside of it unless you return it.
Consider the following function:
def square(n):
return n * n
If we wanted to store 5 squared in a variable called x, we would do:
x = square(5). x would now equal 25.
Common Mistakes
>>> def square(n):
... n * n # calculates then throws away, returns None
...
>>> x = square(5)
>>> print(x)
None
>>> def square(n):
... print(n * n) # calculates and prints, then throws away and returns None
...
>>> x = square(5)
25
>>> print(x)
None
Things to note
print()andreturndo not accomplish the same thing.print()will show the value, and then it will be gone.- A function will return
Noneif it ends without areturnstatement. - When you want to print a value from a function, it's best to return the value and print the function call instead, like
print(square(5)).
!e
def build_deck():
deck = list(range(52))
return deck
d = build_deck()
print(d)
@cursive mica :white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
Then maybe also add a shuffle call in there if you also want that function to produce the deck as shuffled.
That looks better
not sure why the last card is always the same card
column 2 row 7 is always the same
maybe my PickUp() is not correct
i messed up one number in my PickUp() but it's working now
but the last card is always the same card for some reason
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.