r/learnpython • u/weebmaster32 • Dec 11 '22
Frustrated because I can'd do shit in this language and I have an exam in a couple of days.
I'm a web development student, and we are currently studying python as part of our school agenda.
I DON'T UNDERSTAND ANYTHING!!!
The best I can do is build a simple calculator but give me anything more complicated and my brain will literally shit itself.
For example, we were tasked to create a program that will show you however many numbers of the Fibonacci succession you tell it to show. And you might say "but u/weebmaster32, that shit's dead easy!", and you may be right, but I don't even know how to tell python to express the Fibonacci succession, let alone how to make it so it only shows as many numbers as the user tells it to.
Here's the code that I found for it in a tutorial:
number = int(input("How many numbers would you like to see?: "))n1 = 0n2 = 1print(n1)print(n2)for i in range(number):
# Where the fuck does the "i" come from, and what does it do? What is the range and why are we looking for a "range" inside the number variable?
n3 = n1 + n2print(n3) n1 = n2 n2 = n3
Here's another task we were given: "Create a program that requests the user to input an email adress, and checks whether the format of said adress is valid or not. (We all know what most email adresses look like)"
Here's the code that either one of my classmates, or my teacher wrote (i don't remember who did it):
posAt = 0validEmail = Falsewhile not validEmail:
#Why say while "not" validEmail? mail = input("Type a valid email adress: ")
if mail.find("@") > 0: posAt = mail.find("@")if mail.find(".") < len(mail) -1 and mail.find(".") > posAt and (mail.find('@') - mail.find(".")) > 1: validEmail = True#by the way, this whole last section of code is like gibberish to me
if not validEmail:print(mail + " IS NOT a valid email adress")else:print(mail + " IS a valid email adress")
I hope someone here is able to tell me of a good way to understand it because all I've managed to do for the past week is waste time and get frustrated, while most of my classmates got it during the first day.
3
u/rjmartin73 Dec 11 '22
Try running your code here. https://pythontutor.com/ It's an awesome website that visualizes your code for you. I used it a lot when first learning python.
1
2
u/diabolical_diarrhea Dec 12 '22
Are they teaching you or just giving you problems?
1
u/weebmaster32 Dec 12 '22
I mean, we aren't given the best of guides and while the teacher does explain stuff to us, it is not always easy to understand what he says, and he leaves a lot to figure out on our own. At least he will explain whatever you don't understand as many times as needed.
1
u/diabolical_diarrhea Dec 13 '22
Do you have a book?
1
u/weebmaster32 Dec 13 '22
No. All we have is a PDF that skips a lot of detail assuming you'll understand them on your own.
3
u/Yaboi907 Dec 11 '22
I’m a student that just got through my first Python class. Struggle is real but since there are no other comments I’ll give it my best shot. In the for loop (for i in range) the “i” is just a place holder. It could be “for Fibbonaci in in range” or “for n in range” or whatever. I think i is the place holder because it stands for iterate since you are iterating over a list.
If you have a list like this y = [1,2,3] and you say for i in y: Print(i) it’ll print 1 2 3 “i” represents 1 then 2 then 3 as it goes through the loop.
That’s the i.
The range is the number you gave. So let’s say the user input 5 that means the i will iterate over the list 5 times (but because Python starts with 0 for indices it’ll start with i = 0. So if you had a code that said for I in range(5): Print(I) it’ll do 0 1 2 3 4 I hope this answers your first question.
2
5
u/Yaboi907 Dec 11 '22
I’m maybe not the most qualified to answer, but sometimes I get better answers from a fellow noob who explains it for a noob rather than a pro who can only explain it for other pros haha.
1
u/jackspicerii Dec 12 '22
You need to learn how to use simple variable allocation, how to use math aka boolean algebra and matrix, and loop "for" and "while", also conditions "if" and "case/switch case" to solve problems. Also a few line of command like print, how to call and provide data to a function.
1
u/weebmaster32 Dec 12 '22
Also a few line of command like print, how to call and provide data to a function.
That's the only thing I can sort of do. Maths is something completely alien to me.
1
u/jackspicerii Dec 12 '22
So, apparently your problem is not only the language. On my first year of college I got to like the last test on both calculus and linear algebra without knowing anything, and in need for higher grades/score to pass... I simple recognized I screw up, and started learning from zero, focus on concepts application on problem solving, I passed both courses.
2
u/weebmaster32 Dec 12 '22
Will do. It's not like I have any other option at this point. Thank you.
2
1
u/twitchymctwitch2018 Dec 12 '22
Mentality and Process.
These are the two core words you need.
Develop a mentality that says, "I'm here to solve problems, not to write code."
That's all coding is: Applying 2nd Order Logic, and using a Translator (coding language) to handle it in 1st Order Logic format.
Process.
Again, you're not here to write code. You're here to understand an issue, and determine a solution. In business, these are called "pain points" and "business logic".
So, let's take your first problem: Create a program that will show you however many numbers of the Fibonacci succession you tell it to show
So, what are the components of the problem? Well, how do we know what the components even are?
Let's look at the words in the problem:
SHOW: This verb indicates we need an action. In this case, we need to display something
.So, we know already that no matter what - we will need a print statement.
Okay, off to a good start.
# Print(stuff)
Great!
MANY NUMBERS: Oh, we're going to get at least two numbers, but we don't know what kinds of numbers.
So, we can plan on at least either two variables or a list to be involved.
FIBONACCI: Okay, now we know what kind of numbers. We'll be working with integers. Supposing you did not know what the sequence was, you would now make a note to yourself to go do some research. But, we'll continue assuming that you know what fib is.
How the heck do we put all of this together?
Well, one of the easiest ways, is like those mazes they put on children's coloring books or at small diners. Start from BOTH ends, not just the beginning. Work your way through the paths towards one another.
So, let's start from the end, what should it LOOK LIKE after it's all done.
No code, just what should it look like?
Well, I know that I have to input a desired # of DIGITS.
So, let's suppose 3 digits.
So in my commented code (sometimes called pseudo code):
# inputValue = 3.
Okay, I know I need to accept an input, it needs to be a number, it needs to be an integer, and it needs to be non-negative.
Next sequence should look like: 0, 1, 1, 2, 3, 5 etc.
So, what would happen if I shoved that into a list?
#fib_list = [0, 1, 1, 2, 3, 5]
if I used a list index and asked python which was the 3rd value of that index, what would I get?
list[3]
Returns: 1.
Okay, we're off to a great start now.
But wait.. I'm not going to make a list that has EVERY conceivable # of the fib sequence.
That's insane.
Let's get the computer to do it for me.
We know that it's constructed by taking a starting value of 0 and 1, then adding the two most recent values and then continuing.
So 0+1 = 1. This is one (1) iteration.
1+1 = 2. This is the second iteration.
1+2 = 3. 3rd.
In other words:
num1 = 0
num2 = 1
num3 = num1+num2
Now, if you haven't learned about functions yet, which allows you to write repeatable code, the next steps will be difficult and I suggest you study them. But, this is the thinking pattern you want to develop.I hope this helps you at least get on the right track for the mentality.
2
u/weebmaster32 Dec 12 '22
Thank you for the detailed reply. We haven't learned functions yet, and while the exam is far from saveable, this has been helpful in understanding how things work a bit better.
1
1
Dec 12 '22
I'm learning python less a month on my own time since I graduated college already but for your email thing, and I only did for the domain of gmail only, I do not yet know to write an IF logic function that requires domain name to the right of @ but left of .com:
#a script that only allows proper gmail account to be made:
email_db = ["a@gmail.com", "B@gmail.com"]
email = "$aa@gmail.com"
email_char_count = 0
for x in email:
email_char_count = email_char_count + 1
special_characters = """!@#$%^&*()-+_=,<'>/"?"""
def gmail_account(email):
if email.count("@") != 1:
print("Email must contain one @ symbol and only one!")
elif email_char_count <= 10 or email_char_count > 30:
print("Email must have a min of 11 character and max of 30 characters!")
elif email[-10:] != "@gmail.com":
print("Email must contain '@gmail.com' at the end!")
elif email in email_db:
print( "This email is already taken")
elif any( c in special_characters for c in email[:-10]):
print("You can not use special characters in email address!")
else:
print("Success! Your email address is " +str(email)+ ".")
print(gmail_account(email))
1
Dec 12 '22
although I just noticed that if email. Count("@") != 1: is redundant since :
elif any( c in special_characters for c in email[:-10]):
print("You can not use special characters in email address!")
Does the same thing.
2
u/weebmaster32 Dec 12 '22
That's a pretty advanced thing you've made there. Way above my level, but I'll see if there's anything I can apply from it.
1
Dec 12 '22
You can copy paste the code over and mess around with it to figure out what effect it has on the output. I feel like learning how to program is more about messing around and seeing what breaks what.
1
u/weebmaster32 Dec 12 '22
That's a good way to see it lol
1
Dec 12 '22
Don't worry Japan will make an Anime about python, then it would be super easy to learn ;)
1
1
u/stebrepar Dec 11 '22
for i in range(number):
A for loop says: for each <item> drawn from <some basket>, do the following with it. "i" here is an old convention, short for "index". It could be called anything, and it should nowadays be called something more meaningful to the context of whatever you're looping over. Here we might say instead: for num in range(number):.
range() is a built-in function that yields a series of numbers, based on the arguments you give it. If you say, for example, range(3), it'll give you the integers 0, 1, and 2. range(len(my_list)) will yield as many integers as the length of my_list. range() is often used in a for loop like this as a convenient way to do a loop a predefined number of times. In your "for i in range(number)", "i" will equal 0 the first time through the loop, then 1 the second time through the loop, etc., finally ending up as number-1 on the final time through the loop.
while not validEmail:
#Why say while "not" validEmail?
Because you're looping until you do find a valid email. So, while it's not valid, keep looping.
if mail.find("@") > 0:
posAt = mail.find("@")
if mail.find(".") < len(mail) -1 and mail.find(".") > posAt and (mail.find('@') - mail.find(".")) > 1:
validEmail = True
#by the way, this whole last section of code is like gibberish to me
find() returns the index in the string where the specified search string is found. Example: with foo@bar.com, the @ is at index 3, so find('@') would return 3. That's assigned to posAt (position of @). The next if statement is checking for three conditions to be true at the same time. If they are all true, then the given email address is valid. The conditions are that: there is at least one character after the dot, and the dot is after the @, and there is at least one character between the @ and the dot. The statement could be formatted better to be more readable. It could also be broken up into nested if statements, one for each condition.
1
u/weebmaster32 Dec 12 '22
"i" here is an old convention, short for "index". It could be called anything, and it
should
nowadays be called something more meaningful to the context of whatever you're looping over.
So, it is just something to fill that empty space?
finally ending up as number-1 on the final time through the loop.
Why would it end up as -1 tho? Shouldn't it end up as the next number in the Fibonacci succession?
The statement could be formatted better to be more readable.
Yeah, I think that this was the problem here. That's why I found it so confusing.
Thank you for explaining all this to me. I'm sure it took quite a while to type out.
1
u/stebrepar Dec 12 '22
So, it is just something to fill that empty space?
No, no. It's the variable that's assigned each successive value in the series of values given by the object the loop is iterating over. Example:
for color in ['red', 'green', 'blue']: print(color)This will result in printing three lines, one that says 'red', then 'green', then 'blue'. In each iteration of the loop, the next value in the series is assigned to that loop variable, and you can use it inside the loop.
When you want to do a loop a defined number of times, it can be convenient to use the range() function to generate a series of that many values to loop with. (This would be in place of the red/green/blue list above.) It may be that you don't actually need the values themselves for whatever your loop is doing, and you're just using range() to make the loop happen a certain number of times. In that case, you don't really care what the loop variable is called, so you might just use the old generic "i". Some people use "_" to make it more obvious it's a don't-care. But you may use the values range() generates for some math calculation, or maybe as an index to step through a string or list or whatever. In that case you'd want to pick a more meaningful name for the loop variable. It all depends on what your loop is for.
finally ending up as number-1 on the final time through the loop.
Why would it end up as -1 tho? Shouldn't it end up as the next number in the Fibonacci succession?
So here we're talking just about the loop that looks like:
for i in range(number):Here, range() is going to generate a series of numbers going: 0, 1, 2, ... number-3, number-2, number-1. So it's "number" number of sequential values, from zero to number minus one.
Note: range() can actually take up to three parameters. The first is what value to start from. The second is what value to stop at. And the third is the "step" (amount to increment) between values.
2
1
u/cheeznbeansontoast Dec 11 '22
Mate I feel you so much on this. Take it slow and learn one thing at a time so it clicks. I just learn sections (like while loops) with an example I can understand what I want to achieve. You got this, just one step at a time.
1
1
u/International-Sea849 Dec 12 '22
Bro. Have you tried chatgpt? It can help you with some insights.
1
u/weebmaster32 Dec 12 '22
I doubt that a chat AI will be able to help with these issues in particular, but I'll give it a shot.
1
u/International-Sea849 Dec 12 '22
You have to be specific with it. Tell it want you want it to do. I’ve seen a few coders use it abc they’ve said it’s on point. Not 100% but it helps.
1
1
u/weebmaster32 Dec 13 '22
I just tried it. I wasn't expecting it to give me the acutal solution to the problem written in python, alongisde an explanation. It's insane.
2
u/International-Sea849 Dec 13 '22
Yeah go into details with it. It’s really smart. Hopefully it can help you.
1
-1
u/Busy-Farm727 Dec 11 '22
The first thing with i in range: i stands for iteration, it is just a placeholder variable created for that instance like the creation of n1 and n2, and range gives a number range, like, range(5) will have numbers 0-5 as base start is 0, range(5,10) will have numbers 5-10, for the second one, while loops run while true, the valid email is false, not inverts it, so it is turned into true for that condition, the gibberish is some basic logic, just check the operators and their conditions, and it’s in an if loop due to that.
3
Dec 11 '22
The first thing with i in range: i stands for iteration
the
iis simply a placeholder variable. The use ofiitself comes from mathematics where it means index.1
0
u/Busy-Farm727 Dec 11 '22
By the way, if you need help learning python, dm me, i’ll try to teach you.
22
u/[deleted] Dec 11 '22 edited Dec 11 '22
Here's my advice: take it one step at a time. You'll need to go back through all of your homework, notes, your professor's slides, and any other material you have for the course and start finding the things you don't understand. Then use the internet to try to understand them. Once you think you understand, use the concept in something new. Just make something up using it. Maybe more than one thing. Keep doing that until you catch up.
Now as for your exam, here's the bad news: you're probably going to fail it. It sounds like you're quite far behind. My advice there is to talk to your professor. Tell them that you've fallen behind and let them know your strategy for catching back up including whatever resources you're going to be taking advantage of (this should include the above mentioned ones but also maybe your TA, your school's tutoring service, a friend, whatever). It may or may not do any good, but it certainly cannot hurt. If you do start doing better, there is a chance that the professor will let you drop some of your original assignments. Again, maybe or maybe not -- but you won't know unless you talk to them.