#๐Ÿ”’ Python assignment help

86 messages ยท Page 1 of 1 (latest)

desert peakBOT
#

@eternal knot

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.

eternal knot
#

Need help with homework assignment

dreamy iris
#

You can't upload it. Copy and paste relevant text into the chat.

#

@eternal knot

eternal knot
#

oh ok

#

do i just copy paste the code?

dreamy iris
#

Yes

#

!code

desert peakBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

eternal knot
#

it says code is too long

dreamy iris
#

Read the bot message

eternal knot
dreamy iris
#

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

eternal knot
#

what should i do about that?

#

i was provided a starter file and this was already included in that starter file

eternal knot
#

my professor

#

i think

dreamy iris
#

In the meantime, you'd use a list comprehension to make sure that each sub-list is a separate object

#

!listcomp

desert peakBOT
#
List comprehensions

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.

eternal knot
#

so i would use listcomp with play = [[0, 0, 0]] * 7

cursive mica
eternal knot
#

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

tall summit
#

!code

desert peakBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

eternal knot
#
play = [[int() for  in range(3)] for  in range(7)]
tall summit
#

some characters have special meaning for formatting in Markdown, which discord employs

#

italics for example

#

that word is surrounded by *

cursive mica
#

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.

tall summit
eternal knot
#

ok

tall summit
#

yeah and as Carci said, can do [0] * 3 for the inner lists

#

because 0 is immutable

cursive mica
#

Ya, you can't mutate a number, so it doesn't matter if you have multiple references to the same number.

eternal knot
#

this is a program for the 21 card trick do i want to the values in list to be immutable

cursive mica
#

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.

eternal knot
#

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

cursive mica
#

What's wrong?

eternal knot
#

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

cursive mica
#

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.

eternal knot
#

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

cursive mica
#

You want a list full of unique numbers in random order?

eternal knot
#

yeah from 1 to 52

cursive mica
#

Just use random.shuffle.

eternal knot
#

or 0 or 51

cursive mica
#

!e

import random
cards = list(range(52))
random.shuffle(cards)
print(cards)
desert peakBOT
#

@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]
cursive mica
#

That's 0-51 inclusive.

eternal knot
#

would i use deck = list(range(52))?

cursive mica
#

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.

eternal knot
#

i think it'll work as long as put it into the BuildDeck function

cursive mica
#

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?

eternal knot
#

i don't remember how it works tho

cursive mica
#

!return

desert peakBOT
#
Return statement

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() and return do not accomplish the same thing. print() will show the value, and then it will be gone.
  • A function will return None if it ends without a return statement.
  • 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)).
cursive mica
#

!e

def build_deck():
    deck = list(range(52))
    return deck
d = build_deck()
print(d)
desert peakBOT
#

@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]
cursive mica
#

Then maybe also add a shuffle call in there if you also want that function to produce the deck as shuffled.

eternal knot
#

i think im getting it to work

cursive mica
#

That looks better

eternal knot
#

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

eternal knot
#

i believe it works now

#

ty for everyone that helped

#

!close

desert peakBOT
#
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.