Jump to content

🐍 Python

Ripred

Been playing with python, got as far as making a calculator, ya'll think this is fine? seems to work so far, never mind any #comments, just reminding myself of recuring errors...gonna jump into gui stuff soon(keep things interesting), atm this is all just exercises, id like to get to a point where I'm making my own app's, maybe get good enough to take on some freelance stuff but for now I'm just playing, next up looking at inheritance and abstraction 😐, any suggestions for simple projects and or exercises that can give me a well rounded view of abstraction, polymorphism and such? struggling to memorizing syntax looking for some simple interesting things to type out in my free time to help set it in my permanent memory, probably struggling because python reads like english, multiple ways to say the same bloody thing...lol

 

def Math (x,y):
    add = x+y
    sub = x-y
    mul = x*y
    div = x/y
    return add,sub,mul,div

number1 = int(input("first number?: "))
Operator = input("Operator?: ")
number2 = int(input("second number>: "))

ADD,SUB,MUL,DIV = Math(number1,number2)

def calculation(number1,Operator,number2):
    if Operator == "+":
        result = ADD
    elif Operator == "-":
        result = SUB
    elif Operator == "*":
        result = MUL #result, not return :P
    elif Operator == "/":
        result = DIV #(number1,number2)arguments not necessary
    else:
        print("silly rabbit")
    return result

Result = calculation(number1,Operator,number2)
print(Result)

 

 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

That's a... creative way to go about it, but it works.

 

Python's so common that even though I don't use it frequently I can get almost everything done by searching how to do each small bit I need separately. As long as you have reasonable understanding of logic and programming that allows you to split things into basic operations you're gonna find what you need in your favorite search engine.

Plenty of courses too if you actually want to learn the language more consistently.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/24/2024 at 4:03 PM, Kilrah said:

That's a... creative way to go about it, but it works.

 

Python's so common that even though I don't use it frequently I can get almost everything done by searching how to do each small bit I need separately. As long as you have reasonable understanding of logic and programming that allows you to split things into basic operations you're gonna find what you need in your favorite search engine.

Plenty of courses too if you actually want to learn the language more consistently.

I'm taking a python course, along side a CS50 course, id just like some simple coding exercises, to help stick the syntax in memory, understanding the concepts hasn't been much trouble so far, mostly I get tripped up over a misplaced argument, or "" , pycharm has been a big help for reminder's but that's on my personal laptop, which I don't always have with me

 

Quote

That's a... creative way to go about it, but it works.

Wrote it like that for and experiment, didn't realise until then I could stack up variables like that in a single function, wrap em up with a return then unpack them for individual use later, interesting stuff...spose I could write it like this but that would be boring lol

 

 

G = int(input("1st number "))
Op = input("Operator? ")
H = int (input("2nd number "))


def operators(G,Op,H):
    if Op == "+":
        return G+H
    elif Op =="-":
        return G-H
    elif Op =="*":
        return G*H
    elif Op =="/":
        return G/H
    else:
        pass
M = operators(G,Op,H)
print (f"{G} {Op} {H} = {M}")

 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Ripred said:

didn't realise until then I could stack up variables like that in a single function, wrap em up with a return then unpack them for individual use later, interesting stuff

Yup it's pretty cool, also whenever you're limited to one element you can always make a tuple that contains all your objects...

 

Even if it's insignificant and just for experimentation it still hurts to see 3 calculations made for nothing, gives me PTSD from poorly optimized software 🤣

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, Ripred said:

interesting stuff...spose I could write it like this but that would be boring lol

Big part of coding is writing good code. Can't give you a book you should read but NASA has published some guidance and  MISRAC is a starting point.

 

One of the basic rules with coding is: Only introduce complexity/abstraction if the provides something of higher value. In this particular case the math(...) does not yield any benefit. 

 

On 1/24/2024 at 7:16 PM, Ripred said:

Been playing with python, got as far as making a calculator, ya'll think this is fine?

Goto approch would be a PDA instead of prompting the user dozens of times.

People never go out of business.

Link to comment
Share on other sites

Link to post
Share on other sites

I had a programming class in college that started with an assignment similar to this. Each assignment built further on top of the last. First it was "Prompt the user for two numbers to add together." Then it was "Prompt the user for two numbers and an operation (add/subtract/multiply/divide)." Then it was "Do it in a single prompt." Then "Support multiple numbers and operators (ie: 1+2*3)." Lastly, it was "Support inputs like "(1+2)(3*4)/6+(1-2)."

 

Try to keep that final use case in mind as you build out your program. Think about how you might break things into smaller steps and/or handle things recursively.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Kilrah said:

Yup it's pretty cool, also whenever you're limited to one element you can always make a tuple that contains all your objects...

 

Even if it's insignificant and just for experimentation it still hurts to see 3 calculations made for nothing, gives me PTSD from poorly optimized software 🤣

Wait, I can put those into a tuple? Oh my, I'm still trying to get my head around the syntax, I'll get into optimization once I'm more comfortable with what works and doesn't, bare with me in the meantime 🙏

 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/27/2024 at 7:47 AM, CyberNerd2024 said:

Looks like a nice simple program.

 

If you haven't already, check out PEP 8 - the official style guide for Python code.

 

https://peps.python.org/pep-0008/

Neat, this looks very helpful, thanks, bookmarked, I'm trying to work in some loops now, so It continually prompts the user if an invalid input is given, I got the operator to prompt and finish the code once a correct character is given but having an issue getting the numbers input to be checked if they are in fact numbers, GPT3 suggested using .isdigit but I keep getting attribute errors...im pretty comfortable with variables and control flow, if, and, or etc.. trying to work out the various syntax for loops this week, i've gotten basic "for" loops going, just struggling with while loops, I can get them working with initial conditions, inputs and or increments but when I want to ad in more comparison's it all seems to fall apart lol, I think i'm getting tripped up because for's, if's, and while's, etc.. seem to share alot and have interchangeable syntax but not everything is interchangeable apparently...

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Finally getting my head around loops..., on to dictionaries and exceptions, how do we feel about this version? I'm fairly pleased but i just know there is probably some bloody keyword to make it even shorter 😅 also, the operator doesn't get checked until after the 2nd number is given...I can get it to check and re-prompt before using if elif but it was mentioned to me that using more than 3 if/elif's is bad practice, un pythonic i suppose but I couldn't work out getting an input to be checked against a dictionary before moving on to the next input, tried using the same syntax when using if's but kept throwing errors...any suggestions?

def calculations (nA,nB):
    Operator = {"+":lambda x,y:x+y,
                "-":lambda x,y:x-y,
                "*":lambda x,y:x*y,
                "/":lambda x,y:x/y}
    global Op
    while Op not in Operator:
                print ("invalid charactor:")
                Op = input("Operator?: ")
    return Operator[Op](nA,nB)

def get_number(prompt):
    while True:
        try:
            return int(input(prompt))
        except:
            print("Invalid input: ")

nA = get_number("1ts number?: ")
Op = input("Operator?: ")
nB = get_number("2nd number?: ")

result = calculations(nA,nB)
print (f"{nA}{Op}{nB} = {result}")

 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

Alright final version, just been using calculator as a practice exercise, can write a few different versions in my sleep now..., this is about as good as i think I'm going to get it until i start dabbling with building GUI's and breaking my brain on simpler single line comprehension's... , moving on to more advanced list/dictionary manipulation and a dive into file io and manipulation, any suggestion's for projects with more advanced uses of lists and some file io? 

 

 

def get_operator(prompt):
    operators = {"+":lambda x,y:x+y,
                 "-":lambda x,y:x-y,
                 "*":lambda x,y:x*y,
                 "/":lambda x,y:x/y,}
    while True:
        OP = input(prompt)
        if OP in operators:
            return operators[OP],OP
        print("invalid")

def get_number(prompt):
    while True:
        try:
            return int(input(prompt))
        except:
            print("Invalid")

numA = get_number("1st number: ")
Op,Os = get_operator("Operator: ")
numB = get_number("2nd number: ")
print(f"{numA}{Os}{numB} = {Op(numA,numB)}")

 

 

 

 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×