#๐Ÿ”’ Creating A Code

218 messages ยท Page 1 of 1 (latest)

raven pantherBOT
#

@raven oyster

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.

clear field
#

you cant post files they get deleted you can upload them using

#

!paste

raven pantherBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

raven oyster
#

!paste

#

So, i know I start off my importing the libraries!

#

Let me do that real quick... is that fine so far or no?

raven cobalt
raven oyster
#

is this better?

#

And I actually calculated the math... not code it.. lemme show you what i did

#

I need to figure out how to code it though

grim thicket
# raven oyster

By "calculate the following quantities" (Q3), is it expecting exact values?

raven oyster
#

I assume so...

raven cobalt
raven oyster
#

i guess maybe we have all 256 values and we calculate this 256 times, since each number is different. ex. -12.8,-12.7,-12.6, etc.

#

and we need to calculate those quantities.. that is how i understand it

grim thicket
#

I guess so

raven oyster
#

it's a bit tough because if you look at my math, (if its correct), if i input value 1 into my equation, and then value 2 into my equation, the answers i receive are different... thats why i assume thats the case.

#

but rn I am trying to figure out how to input this into code.

grim thicket
#

This is one way to do it: ```py
for n in range(-128, 128):
x = n / 10
print(x)

raven oyster
#

shouldnt it be n/0.1?

grim thicket
#

n * 0.1 or n / 10

#

they both do the same thing

raven oyster
#

sorry give me a minute to figure out why

#

oh i see.

#

i just did another example like 40*0.1=4 and 40/10=4

#

makes sense

grim thicket
#

mhm

raven oyster
#

so here is how my code is looking so far

#

i took a shot on part of part a

#

as well part of part b

#

what do you think so far?

grim thicket
#

first, how familiar are you with matplotlib?

raven oyster
#

tbh not that familar... my professor expects that we know coding prior to taking the class but i have little to no knowledge in python.. i watched a video on a course of python

#

prior to asking these questiosn

grim thicket
#

that's alright

#

!e
The simplest x,y plot looks like this: ```py
import matplotlib.pyplot as plt

xs = [1, 2, 3, 4, 5]
ys = [0, 2, 5, 4, 3]

plt.plot(xs, ys)
plt.savefig("fig.png") # replace with plt.show() in your code

raven pantherBOT
raven oyster
#

Oh, i see! i remember seeing stuff like that in my videos.. only thing is, before I create the plot... how do i insert the value that i received in print to my equation of sinx(ax)

#

and then ig another question is making it use all 256 values..

#

and then i think i can plot!

grim thicket
#

yup

#

are you familiar with using lists?

raven oyster
#

i found this when searching 'list' for python

#

is this what you're referring too?

grim thicket
#

Pretty much

#

we need to build 2 lists (x values and y values) to plot something

raven oyster
#

i see!

#

i assume the x axis is going to be f(x)=sinx(Ax) and y axis is going to be the 256 values.. or vise versa?

grim thicket
#

the x and y values will be interpreted as coordinates

grim thicket
raven oyster
#

oh i see

#

but how do i know the coordinate numbers... dont i need to solve them first... and i cant yk, put a list of every 256 values?

#

i need to code it

#

like i understand the list. but how do i make a list without knowing the values

grim thicket
raven oyster
#

-128,-127,-126,-125.... and so on

#

right?

grim thicket
#

divided by ten

#

-12.8, -12.7, etc.

raven oyster
#

yes

grim thicket
#

then you can use the formula for f(x) to calculate the y coordinate of each point

raven oyster
#

please let me know if thats acceptable

grim thicket
#

not quite

raven oyster
#

can i make a function for this so i can call it into my list or no?

grim thicket
#

you can start with an empty list and add items to it

raven oyster
#

what do you mean?

grim thicket
#

You can add items to a list like this: ```py
x_values = [] # empty list

x_values.append(1)
x_values.append(2)

print(x_values) # [1, 2]

raven oyster
#

append?

grim thicket
#

so if you did that in a loop, you could store all the x values from -12.8 to 12.7

grim thicket
raven oyster
#

oh kind of like a for loop like c++?

#

wait what is append

grim thicket
grim thicket
raven oyster
#

i guess where im kind of confused is ... lets say i make an empty list and then put append(1) and add onto it... do i have to manually do it from -12.7, all the way to 12.7 in one increment

grim thicket
#

you don't have to do it manually, that's what the for loop is for

raven oyster
#

but how do i call the loop into my append.. i need a function right, but how do i make my for loop a function (correct me if im using the wrong terminology)

grim thicket
#

you can call append inside the loop

raven oyster
#

can you provide an example.

grim thicket
#

!e ```py
lst = []
for num in range(5): # 0,1,2,3,4
lst.append(num)
print(lst)

raven pantherBOT
grim thicket
#

see how lst starts out as empty but it gets populated by lst.append(num) inside the loop

raven oyster
#

oh i see! ty

#

why does it start empty. i always see it but i never understoof why

#

i see it in various codes but i never knew why

grim thicket
#

an empty list usually means stuff will be added later

raven oyster
#

oh okay!

grim thicket
#

like in this case, where we could list all the values from -12.8 to 12.7 inside the []

raven oyster
#

that makes a lot of sense

grim thicket
#

but instead we can add them all with a for loop

raven oyster
#

okay! So this is what i have so far ๐Ÿ™‚

#

is it good so far ๐Ÿ™‚

grim thicket
#

why not try run it?

raven oyster
grim thicket
#

yup

raven oyster
#

oh so something is wrong

grim thicket
#

for technical reasons, range can't accept decimal numbers

#

you have to use something similar to the for loop in lines 7-9

raven oyster
#

?

#

lol nope

#

didnt work

grim thicket
#

remember, num / 10 is the singular x value

#

not the entire list of x values

raven oyster
grim thicket
#

looks good

raven oyster
#

that worked!

#

yay

#

now y axis

grim thicket
#

yup

#

so given an x value, can you calculate f(x) ?

raven oyster
#

so ig i have to input the equation but how do i imput the value i received from the equation in the image into my new equation

#

Well yes.

#

if i input what i received for "a", i can receive a equation where if i input an x value, ill be able to calculate for F(X)

grim thicket
#

yup

raven oyster
#

how do i assign the function a to my equation a%b+1

grim thicket
#

I think defining a and b as 3 and 4 is a bit misleading here

raven oyster
#

yeah lol

#

wgat do you recommend i change it too?

#

what

grim thicket
#

I would simply define a = 4%3 + 1

#

so it's the same as in the question

raven oyster
#

okay!

#

ty!

#

lol im suprised i didnt think of that

#

i just made it harder for no reason

#

rn im trying to input the sinc equation

#

give me a minute

#

i tried this but sinc isnt found in the library... can i do math.sin(apix_axis)/apix_axis

#

its bc a = 2 so the equation would be sin(2pix)/2pix

grim thicket
raven oyster
#

oh not math.sin

#

math.sinc

#

this is what i did... please let me know if im right so far... i think it should be correct.

grim thicket
#

you're getting closer

raven oyster
#

Can i please get a hint?

grim thicket
#

so now you can calculate f(x) for a given x value

#

now to do it for each x value in your range

raven oyster
grim thicket
#

not quite... x_axis is a list of all the x values

#

but you need to calculate this for each x value

raven oyster
#

lol i feel like im making it worse

#

am i getting any closer lOL

grim thicket
#

you can use the same loop for both x and y

#

i.e.: ```py
x_axis = []
y_axis = []
for ...:
# calculate x value
x_axis.append(...)
# calculate y value
y_axis.append(...)

raven oyster
#

lol wait

#

im confuse

#

give me a few minutes

#

lol that didnt work

#

am i getting closer or no?

grim thicket
#

the calculation for the y coordinate has to happen inside the loop

#

because it will only work on single x values, not a list of them

raven oyster
grim thicket
#

closer

raven oyster
grim thicket
#

instead of making random changes, think logically about what the program will do

#

hint: what is line 19 doing?

raven oyster
#

well here is how i am approaching it... we have a for loop so that is going from -128 to 128 ... and we are stating in line 18 and 19 that we are dividing those number by 10 and adding a number to that list... and then i insert the equation

grim thicket
#

okay

#

and what y_axis meant to store?

raven oyster
#

its meant to store the equation.

#

but go through the range of (-128,128)

grim thicket
#

it stores the y coordinates of each point in a list

raven oyster
#

how about something like this .. bc im making a list with my equation?

raven oyster
#

would you be able to help me work on this tomorrow? are you busy?

grim thicket
raven oyster
#

to num/10?

grim thicket
#

yup, since that's the current x value

raven oyster
#

wow! im getting it lol

grim thicket
#

you could also assign it to a variable like so: ```py
for num in range(-128, 128):
x = num / 10
x_axis.append(x)
y_axis.append( ... ) # use 'x' instead of 'num/10'

raven oyster
#

my professor added coding tips, do you know what labels is... is it laveling the x axis and y axis title or the title or the grapg... im a bit confused

#

and Oh okay!

#

wait odd im getting an error

grim thicket
#

oh, right

#

hmm, I guess we have to special case that?

raven oyster
#

wdym?

grim thicket
#

sinc(0) = 1, right?

#

that has to be handled separately because the denominator will be 0

raven oyster
#

oh i see

#

you're right

#

if then statement... like if 0 , dont do anything, if not, then use the equation

#

?

grim thicket
#

I think it's worth defining a new function here

grim thicket
raven oyster
#

could you provide me an example on how you would do it in python.. ive done it in c++ but that was A WHILE AGO. I DONT REALLY RECALL IT AS MUCH

#

also are you busy tomorrow?

grim thicket
raven oyster
#

if

grim thicket
#
if condition:
    ...
else:
    ...
raven oyster
#

like i dont know how to say if not 0 ,

#

i know if 0 is if = 0

grim thicket
raven oyster
#

ios not ==

grim thicket
#

Are you looking for !=?

raven oyster
#

no like how do i say if not 0, is it if == 0, 'do not not use equation' and if =0 , 'isnsert equation'

raven oyster
#

i see.. ill just search that up. what function should i make since you recommended it

grim thicket
#

sinc(x)

raven oyster
raven oyster
#

:0 uh i would recommend creating a new help page...

old hemlock
#

Ohhhh mb

#

Wrong side

raven oyster
# grim thicket If-else?

i completed up to part c ... if you can help me with part d and e, that would be great ๐Ÿ™‚

raven pantherBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.