python help

Status
This thread has been locked.

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
i was learning python in a tutorial and i don't understand this part at all,

count = 0
for number in range(1, 10):
if number % 2 == 0:
count += 1
print(number)
print(f"We have {count} even numbers!")

So that displays:
2
We have 1 even numbers!
4
We have 2 even numbers!
6
We have 3 even numbers!
8
We have 4 even numbers!

But the thing is, i don't get what these 2 lines of code are doing?
"
if number % 2 == 0:
count += 1 "

Because if the no number divided by 2 = 0?
and why the count += 1??
 
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
Also this comes up with an error for some reason?

def greet(fist_name, last_name):
print(f"Hi {first_name} {last_name}")
print("Welcome aboard")


greet("Ramo", "Ramo")

Error:
File "c:\Users\UserX PC\Desktop\Python Programming\app.py", line 10, in greet
print(f"Hi {first_name} {last_name}")
NameError: name 'first_name' is not defined[DOUBLEPOST=1567313302][/DOUBLEPOST]never mind i found out i did "fist" name instead of first lol but i still need help for the first thing
 
Last edited:
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)

Sentencings

cool professional words to sound professional :D
Premium
Feedback score
22
Posts
520
Reactions
407
Resources
0
Okay so I'm pretty new to python (been doing it for 3 months but extremely inactive with it) so I'll try to use my java knowledge to answer this question.

Basically on the if statement ("if number % 2 == 0:")
what's it's checking to see is if when the number that is stored within the variable ("number") is divided by 2 if it has a decimal. In the case that the number doesn't have a decimal it will display as 0 hence what happens after the if statement.[DOUBLEPOST=1567313488][/DOUBLEPOST]And what count += 1 means is that it gets the value that is stored within count and it adds a 1 to it. So if for example the value of count is 5, when count += 1 is executed the new value of count will be 6.
 
Last edited:

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
Hmmmm,
But why does it completely disregard decimals?
If the number doesnt have a decimal it will display as 0?
But wouldnt it be the opposite, if it doesnt have a decimal it will display as the number?
Also why does the count need to add one?
 
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)

Sentencings

cool professional words to sound professional :D
Premium
Feedback score
22
Posts
520
Reactions
407
Resources
0
Hmmmm,
But why does it completely disregard decimals?
If the number doesnt have a decimal it will display as 0?
But wouldnt it be the opposite, if it doesnt have a decimal it will display as the number?
Also why does the count need to add one?
This is what the % operator does:
Divides and returns the value of the remainder.

The count is needed for the amount of times that there is an even number. That is displayed within the print statement print(f"We have {count} even numbers!")
 

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
Fuck i thought python was easy
 
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)

Sentencings

cool professional words to sound professional :D
Premium
Feedback score
22
Posts
520
Reactions
407
Resources
0
It is, I'm just bad at explaining, my bad. Just disregard all I said.
 

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
Nah your great its just confusing that's all.
I was dumb and i went straight to learning java which is 100x harder than python so know im getting around python hopefully!
 
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)

Zyger

Middleman
Supreme
Feedback score
414
Posts
2,209
Reactions
2,615
Resources
0
% is the modulo operator.

"In computing, the modulo operation finds the remainder after division of one number by another (called the modulus of the operation). Given two positive numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor."

The code is saying that if there is no remainder (0) after dividing by 2, then this means that it must be an even number, and increases the count variable (of even numbers) by 1.
 
Last edited:

Aureus

aurum est potestas
Feedback score
8
Posts
95
Reactions
72
Resources
0
Fuck i thought python was easy
Doesn't matter what language you use, the modulus operator is in use for all object-oriented languages (Python, Java, C++, etc.). Modulus is literally just a remainder calculator. For example, 15 % 3 = 0 because 15 is perfectly divisible by 3. On the other hand, 15 % 4 is 3 because 4 only goes into 15 three times, and has a remainder of 3.

Also count +1 is the increment because you want to keep track of how many numbers are divisible by 2. Everytime a number IS divisible by 2, the count increments (goes up by 1) to keep track. If it is odd (or when number % 2 == 1), it does NOT increment.
 

croissant222

Feedback score
0
Posts
439
Reactions
151
Resources
0
Doesn't matter what language you use, the modulus operator is in use for all object-oriented languages (Python, Java, C++, etc.). Modulus is literally just a remainder calculator. For example, 15 % 3 = 0 because 15 is perfectly divisible by 3. On the other hand, 15 % 4 is 3 because 4 only goes into 15 three times, and has a remainder of 3.

Also count +1 is the increment because you want to keep track of how many numbers are divisible by 2. Everytime a number IS divisible by 2, the count increments (goes up by 1) to keep track. If it is odd (or when number % 2 == 1), it does NOT increment.
"for all object-oriented languages"
Idk why u mention this cause being oop has no significance to needing a modulo operator
 

Samuel

The most serious person ever.
Supreme
Feedback score
33
Posts
2,210
Reactions
1,572
Resources
0
The % operator will divide the number on the left, by the number on the right, then give you whatever the "remainder" is.

e.g. 2%1 means "divide 2 by 1, then give me the remainder". This would give you 0, since 1 fits into 2 PERFECTLY, twice.

Another example is 2%1.5, meaning "divide 2 by 1.5, then give me the remainder". This would give you 0.5 because 1.5 fits into 2 once, but then there is 0.5 left over and we cannot divide again.

count += 1 means "take the value of the variable count, then add one. once that's done, make the value of count the result of this calculation". It's the equivalent of doing this:
Code:
count = 0
newcount = count + 1 // newcount = 0 + 1
count = newcount // count = 1
print(count) // 1
Or better yet:
Code:
count = 0
count = count + 1 // count = 0 + 1
print(count) // 1
 
Last edited:

Samuel

The most serious person ever.
Supreme
Feedback score
33
Posts
2,210
Reactions
1,572
Resources
0
Ramo - not sure if this helps you, but I've described each line for you:
upload_2019-9-1_17-52-52.png

This isn't a perfect description because technically "starting at 1, count up to 10" isn't what "for number in range(1,10)" is doing in the background, but it gets the point across for now.

Edit: I made an oopsie! It goes from 1 to 9 actually. Python isn't my mother tongue, hope y'all understand. <3
 

Attachments

  • upload_2019-9-1_17-52-52.png
    upload_2019-9-1_17-52-52.png
    99.4 KB · Views: 70
Last edited:

croissant222

Feedback score
0
Posts
439
Reactions
151
Resources
0
Ramo - not sure if this helps you, but I've described each line for you:
View attachment 264270
This isn't a perfect description because technically "starting at 1, count up to 10" isn't what "for number in range(1,10)" is doing in the background, but it gets the point across for now.
Just saying it goes from 1 to 9 not 10
 

Niteburn

Supreme
Feedback score
12
Posts
595
Reactions
487
Resources
0
% is the almost the exact same as division, but it gives you the remainder (elementary school math)
+= is a addition assignment operator that takes the value of the left operand and adds the right operand together.

I would answer the other questions but I don't know Python and it's is really abstracted. It seems that people are trying to over-complicate the simple question you are asking, I would post questions on Stack Overflow, not here.
 

Ramo

Banned
Feedback score
6
Posts
177
Reactions
13
Resources
0
Now im confused with classes and what self means but i appreciate all the help!
I've got a question for y'all.
So you know how there is pygame?
like how u need that to make a game.
Does importing pygame bring new code to python like stuff that wouldn't exist for it.
So like there are so many sub imports for python for whatever u are doing?[DOUBLEPOST=1567427203][/DOUBLEPOST]like the pygame.display.set_mode((500, 500))
thats not python code right?
thats pygame code?
 
Banned forever. Reason: Scamming (https://www.mc-market.org/threads/567487)
Status
This thread has been locked.
Top