#π i made a very shitty average calculator and i know there are better ways to do it but need help
346 messages Β· Page 1 of 1 (latest)
@sullen heron
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.
improve it as in the code
we cant really suggest improvements to code that we dont see
what was the format thing again
!code ?
def avarage_calc2(a, b):
return a + b
def avarage_calc3(a, b, c):
return a + b + c
def avarage_calc4(a, b, c, d, e):
return (a + b + c + d) / e
print('Welcome! please enter your subject below')
subject = input(' > ')
if subject != '':
print('please enter the amount of grades you wanna enter')
grade_number = input(' > ')
if grade_number == '1':
print('???')
elif grade_number == '2':
print('enter below!')
grades = int(input(' > '))
grades2 = int(input(' > '))
print('the avarage of ', str(subject), '=', grades, '+', grades2, '=',avarage_calc2(grades, grades2,))
elif grade_number == '3':
print('enter below!')
grades3 = int(input(' > '))
grades4 = int(input(' > '))
grades5 = int(input(' > '))
elif grade_number == '4':
print('enter below! WARNING: at last answer enter amount of grades u entered')
grades6 = int(input(' > '))
grades7 = int(input(' > '))
grades8 = int(input(' > '))
grades9 = int(input(' > '))
grades10 = int(input(' AMOUNT OF GRADES > '))
print('the avarage of ', str(subject), '=', grades6, '+', grades7, '+',grades8, '+',grades9, '=', avarage_calc4( grades6, grades7, grades8, grades9, grades10))
i js hopped on my pc and typed whatever looked logical
its not finished i know that
i was just brainstorming if i could like array the inputs then / it by the array index?
sure, use a loop or some other construct for repeating things
no sensible program hardcodes the code for each amount of inputs
huh
do you know how to do forand/or while loops?
i know how to start one and how it works
but im ngl i have no idea how to implement a loop into this
so, what rules do you want to apply to the input?
what should be the minimum and maximum allowed values?
Great way to start coding, even greater u came here seeking advice to improve it, good job 
well this code is about grades so im guessing min input is 1 and max is 10
i didnt wanna watch a 5 hour video about python and then know nothing so i js watched a 10 minute video of someone explaining the VERY basics and then i got to work
so, then you want to convert the input to a integer (whole number) between those two numbers (inclusive)
One of rare cases where a tutorial might just work out for some1 huh
huh wdym im a little slow
yeah, i would stay away from the really long videos
i wrote everything he explained like variables datatypes loops etc and then js did wtv
with prior javascript knowledge but i forgot most of that bc during lectures i js chatgped it all
evenything that you input at the input() prompt is returned as a string of text, you can't do any math on it directly
i have int(input())
you need to convert it to a number, and for whole numbers that is int (an integer)
So from that u prob learned that if u want to learn u shouldnt rely on ai like chatgpt i hope 
i did its exactly why im here rn lol
i regret using chatgpt for everything so im here asking to stay away from it
good decision and learning π
i try my best
but the code i wrote is very unconventional im guessing
hope i wrote that correctly
alot of repeating
yeah, we are going to address that
if i learned 1 thing from tutorials its minimum code for max output
using loops
For beginner code i would give it 8 or 9 out of 10
β€οΈ
Just because it isnt how experienced people would do it doesnt mean its bad code
well i know if i finished the code it does the job
but if i try to implement more advanced stuff ill get more advanced
usually i hate coding but after hearing my symfony linux classes i wanted to code more
you want to have int() around this one as well
grade_number = input(' > ')
```and then we'll change the if statements
i also wanna go different directions with IT
oh right ty
since you are asking for the number of something we do want to convert it to a number as well
guessing i should have int(input()) infront of everything where an integer is needed
then there are many ways to solve the loop
Idea was but its a bit redundant, u can print non-strings
to start off with, but later you probably want to do it a slightly different way for proper error handling, but that is for when you are at that level of coding
π
alrighty
Dw a lot of beginners dont get that at first either 
input() returns a string by default, so you don't need to convert that to str manually
i felt like i was in a hacker movie when i wrote that str()
ah
that makes sense
thats probably also why if i typed 2 4 5 i got back 245
before adding int
solved that by peeking at the calculator i built by snooping on google
how long have you been learning python?
i'm asking because other then the repetitions i think it's not bad at all for a beginner
well i started learning python 3 or 4 days ago
all by myself
in school i took a year of javascript classes
that is very good progress in that short amount of time π
ah, that helps
tyt y
having experience from another programming language
actually 6 months because the other 6 months were spent on php
oh, so you have two other languages under your belt π
Php is def a fun language
oh, doing it with ai, that way you don't learn very much
i see where you are coming from now
yeah
i know very little about javascript and how to work with itt because i never did
good decision to skip ai this time around
i know what does what
but i couldnt implement it myself
if i use ai its for like a random line that has an error and im completely lost
but yeah learning python was out of nowhere
have you worked with lists in python yet?
ever since ive been replaying watchdogs 2 i wanted to learn more cyber security and hacking and stuff
but i also wanted to learn python
to get into coding more
i can send my 3 little projects
those are the only things ive done so far
lets concentrate on this one project for now
So what u know about lists
nothing pretty sure
so, we should be about here right now
print('Welcome! please enter your subject below')
subject = input(' > ')
if subject != '':
print('please enter the amount of grades you wanna enter')
grade_number = int(input(' > '))
yes
you want to start with a loop that does something a certain number of times
let's go with a for loop to start with
do you know about range()?
for i in range(4):
print()
prints it 4 times
right
i feel like a master programmer
great, you can use this for the input of the grades as well
we don't need the number right now, so you can change the i to _ in there, it's usually used as a through away variable, like "we are not going to use this value" and then instead of 4 in there, you want the variable with the answer from the last question
what do you think that code look like?
if you can give me that line of code
hmmmm
because we want to ask for the input for the grade that number of times that they input just before that loop
so no using i
you can, but you don't really need it (that's why we skip it and put something else there instead)
ill do it for simpicity
lemme think
where should i add the for loop?
in the elif
..?
you can have the i there for now if you want to
but you won't really need that variable or that value
as you just want to do the loops that number of times
and don't really care which iteration of the loop you are currently in
Well for starters, lets think of a list as a street, our elements are our houses and our indexes are the houses' adressess
# the [] and , are important
some_list = [1, 2, "3", 4]
So here we have 4 elements (1,2,"3" and 4) and respectively they have the indexes 0,1,2 and 3 (always integers starting the count from 0)
# indexes our list with 0 to get 1st element
print(some_list[0]) # will print 1
Im post this for later since i finished writing it but focus on that loop for now

okay i think im over complicating the loop
brainfart
i actually have no idea how to make a for loop for this instance
i think im thinking too hard
oh so an array?
If u know arrays then they close enough ig
They a lil different but very similar
fair
wheres the other mr
i need him to help me with the loop
π₯Ή
because i have no idea
Do u see any line(s) that is basically spammed over and over
Replace that with a loop
elif grade_number == '2':
print('enter below!')
for i in range(2):
grades = int(input(' > '))
like this?"
On right track, u need to indent stuff to be inside loops
Just like u do with if
Also this doesnt store / do anything with the input
i also heard indents are important for python
So once user inputs once, its gonna be lost next loop iteration as u just ask them again for input and store on same var
take this loop and modify it a little bit
isnt that what i did above
indents are used in python instead of curly braces to group lines together into a block of code
many languages use curly braces, others use a specific word like ifreversed as fi (posix-like shell scripting for example) others use something like done, end or endif, python uses the indentations instead
U gonna be very well acquainted with fi approach if u gonna be on linux
well i have to learn linux for school
i also wanna dive into linux myself so im glad we get the classes
i am on windows yes
it's good enough for many things
wsl --install archlinux u do that by just typing this into console
windows subsystem for linux
Wsl is literally a mini linux instance/terminal but running on ur windows
ubuntu will probably be more user friendly for a beginner
isnt archlinux like linux final boss
no, there are others that are more like a final boss than that
Nah
Installing arch from scratch is def not something i recommend
But getting used to it from wsl would be surprisingly not bad
"linux from scratch" is a dist where you do everything yourself, it's really useful to learn the ins and outs of linux when you already know quite a lot for example
Then there is people like me who use gentoo
gentoo is more "from scratch" then arch for example, many gentoo people have moved over to arch now though
Which is the 2nd last boss or so to speak
damn
..
idk i wanna use linux to look like a hacker
thats a joke
im interested in linux
isnt kalilinux used for hacking tho
youtube autoplay does wonders
More specfiically pentesting
you also have things like NixOS and Guix
okay where does a baby coder like me start with linux
And on the other end of spectrum u got distros like uwuntu
yeah, many people come here with kali linux installed π€¦
don't do that
okay i wont
mint is apparently a good starting distro
Get ubuntu or debian if u want to daily drive it like u would with windows else imo get archlinux on wsl
it's ubuntu with more things out of the box
π
but ubuntu will be fine too
well its not like i wanna use linux for gaming or everyday things necessarily
i just wanna know more
personally i don't like ubuntu much, i'll rather just use debian or something else, but i guess it's good for beginners
fedora is nice too
Then i suggest installing archlinux on wsl
Arch wiki is hella nice
archlinux it is then
fedora = for developers by developers
started trying to fix my code left as a member of linux
that was their tagline for a long time
Its what i shoved on my 2015 macbook air when i got sick of macos
Do u know what virtual machines are
Basically what wsl is
that was the word i was looking for i js forgot it
Except it doesnt take huge resources to run
aaaaah
okay how to wsl myself into archlinux
this took such a big turn of events im dying
Just install it with command i said earlier
it uses a light version of microsofts hyper-v for virtualization
Then u can just type wsl to enter
The only thing a lil weird about archlinux wsl image is that it doesnt come with a user but thats easy to fix
anyways, shouldn't we be working on your code? π
Oh right this #1035199133436354600
why isnt wsl opening
grr
why does it look like a penguin
antartica animal
did i break wsl
so, the loop, it should look something like
print('enter below!')
for _ in range(grade_number):
grade = int(input(' > '))
```but right now we aren't saving the numbers besides the very last one as we are overwriting that variable over and over
so you would want a list and the append that to the list for each iteration
yes i thought about that
but with an array
where u append the grades into an array
as you wanted
print('enter below!')
```before all the inputs, but only once, we but that outside of the loop, before the loop
Try it
no.
then we want a list too and append things to the list
but to focus on the loop, this is what range()gives you
!e
print(list(range(5)))
:white_check_mark: Your 3.13 eval job has completed with return code 0.
[0, 1, 2, 3, 4]
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | >
002 | >
003 | >
004 | >
005 | >
we get the prompt printed five times
we didn't need the number here, so i'm "throwing it away" by assigning it to _ (it's only by convention, it's not really thrown away, it's assigned to a variable named _, but we typically don't use it and just use it to communicate that "we are not going to use this value anywhere in the code")
so 5 is assigned to _?
instead of just printing the prompt we use input() instead to be able to ask the user for input, but the Python bot here can't handle input() as there is no real terminal for it to read from
ah
elif grade_number == '2':
print('enter below!')
for _ in range(grade_number):
grades = int(input(' > '))
yes, the first time 0 is assigned to _, the second time 1 is assigned to it and so on until 4, which is the fifth value as it starts from 0
this is what i copied from u so far.
you don't need the if statement there anymore
so you don't need to check the number they input
OH
we can add some checks to have limits on the value later, but for now we don't need it
it will run as many times as they tell it to
this is why you want a loop
ahh so i can delete all other statements
and in python you generally use lists rather then arrays unless you are doing very specific things
yeah, and the repetition of code
this is why we use a loop here instead
do "this" (the indented code block under the for loop) x number of times
I still dont get what that part is accomplishing
we can do it right away if you are just going to calculate it and never use the values one by one
well i need to use math to calculate the average no?
There is 2 sum functions and then a sum and a division
i'm guessing calculating the average grade
no need really
just add to a total
so, you want a total = 0 above the loop
wait i sparked a thought
what if i add everything and then devide by the GRADE_NUMBER
im so smart
and then you can add the grade to the total within the loop for each iteration
because you need something to hold the values that they put in, either a list that you later sum up or a total that you can calculate as you go
ah fair
then you take the total and divide it with grade_number
yes
smart
also when i press enter sometimes it registers double is that a pycharm thing or a keyboard thing
so you have the basic syntax of
total = total + grade
```or you can do both operations at the same time with the combined operator `+=` (no space in between them and in that specific order) which looks like
```py
total += grade
whats the difference
no difference other then more compact syntax when the variable that you assign to is the same as the variable in-front of the operand like you have in the first one there
ah
print('enter below!')
for _ in range(grade_number):
grades = int(input(' > '))
total += grades
what i have rn
so, we should be about here right now
print('Welcome! please enter your subject below')
subject = input(' > ')
if subject != '':
print('please enter the amount of grades you wanna enter')
grade_number = int(input(' > '))
total = 0
print('enter below!')
for _ in range(grade_number):
grade = int(input(' > '))
total += grade
print(total / grade_number)
something like that at least
there is a lot more we could do to make this code better, but it should be a good start for now
yues ty
youre so goated
im glad my initial code wasnt that bad either
but this cleared it up for me
and made loops more clear
off topic
do yk why wsl wont start anymore
the best way to learn something is to do mistakes
you don't learn very much by success
at least not until you made a lot of mistakes first to get there
ofcourse
..?
well i gtg anyways thats a bother for later
if u know why please dm me incase this little chat gets closed or deleted
THANK YOU and have a nice day!!!!!!!!!!!
would need more time too look into that
chats are archived as read-only after you close them or they are automatically closed by the Python bot after an hour without any new messages in the thread
but you can save a link to the thread or a specific message and read it any time you like later on
you're welcome and thanks, you too
welcome back here to open a new help thread any time you have another python related question
@sullen heron for reference, here is a version that gathers the answers up into a list and sums them
print('Welcome! please enter your subject below')
subject = input(' > ')
if subject != '':
print('please enter the amount of grades you wanna enter')
grade_number = int(input(' > '))
grades = [] # create an empty list
print('enter below!')
for _ in range(grade_number):
grade = int(input(' > '))
grades.append(grade) # append each grade to the list
print(sum(grades) / grade_number) # sum up the grades in the list and divide the sum with the number of grades to get the average
```but for just calculating an average this isn't really needed unless you are going to use the values from each answer for something else (like presenting them or something), so for now i would prefer the earlier implementation
i would put this into a function and turn the if statement into a if-guard statement instead for example
then add an if-guard to the grade_number to check that it's within the range of allowed numbers
just for starters
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.