#πŸ”’ 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
#

so i just made a very shitty average calculator and as the title says would like to know how to improve it

brave lanternBOT
#

@sullen heron

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.

sullen heron
#

improve it as in the code

hollow patio
sullen heron
#

what was the format thing again

hollow patio
#

!code ?

brave lanternBOT
#
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.

sullen heron
#
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?

hollow patio
#

sure, use a loop or some other construct for repeating things
no sensible program hardcodes the code for each amount of inputs

sullen heron
#

huh

celest jackal
#

do you know how to do forand/or while loops?

sullen heron
#

i know how to start one and how it works

#

but im ngl i have no idea how to implement a loop into this

celest jackal
#

so, what rules do you want to apply to the input?

#

what should be the minimum and maximum allowed values?

abstract lily
sullen heron
sullen heron
celest jackal
abstract lily
#

One of rare cases where a tutorial might just work out for some1 huh

celest jackal
sullen heron
#

with prior javascript knowledge but i forgot most of that bc during lectures i js chatgped it all

celest jackal
sullen heron
#

i have int(input())

celest jackal
#

you need to convert it to a number, and for whole numbers that is int (an integer)

abstract lily
sullen heron
#

i regret using chatgpt for everything so im here asking to stay away from it

celest jackal
sullen heron
#

i try my best

#

but the code i wrote is very unconventional im guessing

#

hope i wrote that correctly

#

alot of repeating

celest jackal
#

yeah, we are going to address that

sullen heron
#

if i learned 1 thing from tutorials its minimum code for max output

celest jackal
#

using loops

abstract lily
sullen heron
#

❀️

abstract lily
#

Just because it isnt how experienced people would do it doesnt mean its bad code

sullen heron
#

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

celest jackal
#

you want to have int() around this one as well

grade_number = input(' > ')
```and then we'll change the if statements
sullen heron
#

i also wanna go different directions with IT

celest jackal
#

since you are asking for the number of something we do want to convert it to a number as well

sullen heron
#

guessing i should have int(input()) infront of everything where an integer is needed

celest jackal
#

then there are many ways to solve the loop

sullen heron
#

was the str(subject) a smart move

#

i felt proud after writing that

abstract lily
#

Idea was but its a bit redundant, u can print non-strings

celest jackal
abstract lily
celest jackal
#

input() returns a string by default, so you don't need to convert that to str manually

sullen heron
#

i felt like i was in a hacker movie when i wrote that str()

sullen heron
#

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

celest jackal
#

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

sullen heron
#

well i started learning python 3 or 4 days ago

#

all by myself

#

in school i took a year of javascript classes

celest jackal
#

that is very good progress in that short amount of time πŸ‘

sullen heron
#

but i didnt do alot of coding with js

#

i did it all with ai

celest jackal
#

ah, that helps

celest jackal
#

having experience from another programming language

sullen heron
#

actually 6 months because the other 6 months were spent on php

celest jackal
#

oh, so you have two other languages under your belt πŸ‘

abstract lily
#

Php is def a fun language

sullen heron
#

i know html css js and php

#

its not like im a master at those

celest jackal
#

oh, doing it with ai, that way you don't learn very much

#

i see where you are coming from now

sullen heron
#

yeah

#

i know very little about javascript and how to work with itt because i never did

celest jackal
#

good decision to skip ai this time around

sullen heron
#

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

celest jackal
#

have you worked with lists in python yet?

sullen heron
#

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

sullen heron
#

those are the only things ive done so far

celest jackal
#

lets concentrate on this one project for now

sullen heron
#

oops

#

okay lets do that

abstract lily
sullen heron
#

nothing pretty sure

celest jackal
#

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(' > '))
sullen heron
#

yes

celest jackal
#

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()?

sullen heron
#

for i in range(4):
print()

#

prints it 4 times

#

right

#

i feel like a master programmer

celest jackal
#

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

sullen heron
#

hmmmm

celest jackal
#

because we want to ask for the input for the grade that number of times that they input just before that loop

sullen heron
#

so no using i

celest jackal
#

you can, but you don't really need it (that's why we skip it and put something else there instead)

sullen heron
#

ill do it for simpicity

#

lemme think

#

where should i add the for loop?

#

in the elif

#

..?

celest jackal
#

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

abstract lily
# sullen heron nothing pretty sure

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

sullen heron
#

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

abstract lily
#

They a lil different but very similar

sullen heron
#

fair

#

wheres the other mr

#

i need him to help me with the loop

#

πŸ₯Ή

#

because i have no idea

abstract lily
#

Replace that with a loop

sullen heron
#
    elif grade_number == '2':
        print('enter below!')
        for i in range(2):
        grades = int(input(' > '))
#

like this?"

abstract lily
#

Just like u do with if

#

Also this doesnt store / do anything with the input

sullen heron
#

i also heard indents are important for python

abstract lily
#

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

celest jackal
sullen heron
#

isnt that what i did above

celest jackal
sullen heron
#

oh

#

ohhhh

#

i was wondering how it knew which codes are together without like {}

celest jackal
#

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

abstract lily
#

U gonna be very well acquainted with fi approach if u gonna be on linux

sullen heron
#

well i have to learn linux for school

#

i also wanna dive into linux myself so im glad we get the classes

celest jackal
#

are you on windows right now?

#

in that case just start by using wsl

sullen heron
#

i am on windows yes

celest jackal
#

it's good enough for many things

sullen heron
#

wsl?

#

im still lost on the loop..

abstract lily
celest jackal
#

windows subsystem for linux

abstract lily
celest jackal
#

ubuntu will probably be more user friendly for a beginner

sullen heron
celest jackal
#

no, there are others that are more like a final boss than that

abstract lily
#

Installing arch from scratch is def not something i recommend

#

But getting used to it from wsl would be surprisingly not bad

celest jackal
#

"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

abstract lily
#

Then there is people like me who use gentoo

celest jackal
#

gentoo is more "from scratch" then arch for example, many gentoo people have moved over to arch now though

abstract lily
#

Which is the 2nd last boss or so to speak

sullen heron
#

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

abstract lily
celest jackal
#

you also have things like NixOS and Guix

abstract lily
#

And its awful for daily driving

#

Not even made for daily driving

sullen heron
#

okay where does a baby coder like me start with linux

abstract lily
celest jackal
#

yeah, many people come here with kali linux installed 🀦
don't do that

sullen heron
#

okay i wont

celest jackal
#

mint is apparently a good starting distro

abstract lily
sullen heron
#

delete system 32

#

sudo

celest jackal
#

it's ubuntu with more things out of the box

sullen heron
#

😎

celest jackal
#

but ubuntu will be fine too

sullen heron
#

i just wanna know more

abstract lily
#

Why did i suggest debiab

#

I meant fedora

sullen heron
#

i wanna dive into the world of computers

#

lowk be a coding geek

celest jackal
#

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

abstract lily
#

Arch wiki is hella nice

sullen heron
#

archlinux it is then

celest jackal
#

fedora = for developers by developers

sullen heron
#

started trying to fix my code left as a member of linux

celest jackal
#

that was their tagline for a long time

abstract lily
sullen heron
#

is wsl like a mini computer

#

isnt linux an os

abstract lily
sullen heron
#

yes

#

computer in computer

abstract lily
#

Basically what wsl is

sullen heron
#

that was the word i was looking for i js forgot it

abstract lily
#

Except it doesnt take huge resources to run

sullen heron
#

aaaaah

#

okay how to wsl myself into archlinux

#

this took such a big turn of events im dying

abstract lily
celest jackal
#

it uses a light version of microsofts hyper-v for virtualization

abstract lily
#

Then u can just type wsl to enter

sullen heron
#

its starting

#

it closed itself

abstract lily
#

The only thing a lil weird about archlinux wsl image is that it doesnt come with a user but thats easy to fix

celest jackal
#

anyways, shouldn't we be working on your code? πŸ˜†

sullen heron
#

right..

#

i gotta leave in 25 minutes btw

#

..

abstract lily
sullen heron
#

why isnt wsl opening

#

grr

#

why does it look like a penguin

#

antartica animal

#

did i break wsl

celest jackal
#

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
sullen heron
#

could u also explain like why u do some things

#

helps with understanding

celest jackal
#

so you would want a list and the append that to the list for each iteration

sullen heron
#

yes i thought about that

#

but with an array

#

where u append the grades into an array

celest jackal
#

as you wanted

print('enter below!')
```before all the inputs, but only once, we but that outside of the loop, before the loop
abstract lily
sullen heron
#

no.

celest jackal
#

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)))
brave lanternBOT
celest jackal
#

so if we put that in a loop we instead get

#

!e

for _ in range(5):
    print(" > ")
brave lanternBOT
celest jackal
#

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

sullen heron
#

so 5 is assigned to _?

celest jackal
#

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

sullen heron
#

ah

#
    elif grade_number == '2':
        print('enter below!')
        for _ in range(grade_number):
            grades = int(input(' > '))
celest jackal
sullen heron
#

this is what i copied from u so far.

celest jackal
#

you don't need the if statement there anymore

#

so you don't need to check the number they input

sullen heron
#

OH

celest jackal
#

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

sullen heron
#

ahh so i can delete all other statements

celest jackal
#

and in python you generally use lists rather then arrays unless you are doing very specific things

celest jackal
#

this is why we use a loop here instead

sullen heron
#

ahhhhhhhhhhhhhhh

#

my code just became half the size

celest jackal
#

do "this" (the indented code block under the for loop) x number of times

sullen heron
#

ahhh

#

and how do i do the math part..

#

same way?

abstract lily
celest jackal
#

we can do it right away if you are just going to calculate it and never use the values one by one

sullen heron
abstract lily
#

There is 2 sum functions and then a sum and a division

celest jackal
#

i'm guessing calculating the average grade

#

no need really

#

just add to a total

#

so, you want a total = 0 above the loop

sullen heron
#

wait i sparked a thought

#

what if i add everything and then devide by the GRADE_NUMBER

#

im so smart

celest jackal
#

and then you can add the grade to the total within the loop for each iteration

sullen heron
#

why?

celest jackal
#

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

sullen heron
#

ah fair

celest jackal
#

then you take the total and divide it with grade_number

sullen heron
#

yes

#

smart

#

also when i press enter sometimes it registers double is that a pycharm thing or a keyboard thing

celest jackal
#

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
sullen heron
#

whats the difference

celest jackal
#

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

sullen heron
#

ah

#
    print('enter below!')
    for _ in range(grade_number):
             grades = int(input(' > '))
             total += grades
#

what i have rn

celest jackal
#

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

sullen heron
#

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

celest jackal
#

the best way to learn something is to do mistakes

sullen heron
#

make break repair

#

something along the lines

celest jackal
#

you don't learn very much by success

#

at least not until you made a lot of mistakes first to get there

sullen heron
#

ofcourse

sullen heron
#

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!!!!!!!!!!!

celest jackal
sullen heron
#

ofcourse

#

it happened after the wsl line and it installed shit

celest jackal
#

but you can save a link to the thread or a specific message and read it any time you like later on

celest jackal
#

@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

brave lanternBOT
#
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.