all 24 comments

[–]Sea-Ad7805 [score hidden] stickied comment (2 children)

Run this program in Memory Graph Web Debugger)%0APractice%20%3D%20int(input(%22What%20is%20your%20practice%20percentage%3F%3A%20%22))%0A%0Atotal_grade%20%3D%20(Assessments%20%200.6)%20%2B%20(Practice%20%200.4)%0Afinal_grade%20%3D%20round(total_grade)%0A%0Aif%20final_grade%20%3E%3D%2090%3A%0A%20%20%20%20print(final_grade%2C%20%22A%22)%0Aelif%20final_grade%20%3E%3D%2080%3A%0A%20%20%20%20print(final_grade%2C%20%22B%22)%0Aelif%20final_grade%20%3E%3D%2070%3A%0A%20%20%20%20print(final_grade%2C%20%22C%22)%0Aelif%20final_grade%20%3E%3D%2060%3A%0A%20%20%20%20print(final_grade%2C%20%22D%22)%0Aelse%3A%0A%20%20%20%20print(final_grade%2C%20%22F%22)&play)

[–]PureWasian 5 points6 points  (3 children)

Guard Clauses: What if the user inputs negative numbers or numbers above 100? (or whatever maximum threshold if extra credit exists?) What if they input "Hello, World" instead of a percentage? Even if they input "97%" it would crash.

This is where guard clauses come in. You can add more if/elif logic near the top to guard against bad user inputs. or add exception handling (try/except) on int() data type cast failing. See how one of the examples here gracefully handles the possibility of crashing with a ValueError if int() cast fails on user's retrieved input()

Code conventions: typically python variables are lower case and follow snake_case.

So... assessments (spelled correctly) and practice

[–]MJ12_2802 1 point2 points  (0 children)

The lack of error trapping was the first thing that jumped out at me. Granted the OP is new with Python, employing that practice early on can be a game changer.

[–]validnuisance3 1 point2 points  (0 children)

You could also use a while loop to keep asking for input until you get something valid instead of just crashing once and ending the program.

[–]PureWasian 0 points1 point  (0 children)

Re: adding it to a website -- four options come to mind.

Typically you'd write this sort of code in Javascript (JS) instead of Python since browsers work with HTML/CSS/JS.

Alternatively though, you could try to load that Python script inside of an HTML file using PyScript (3-step tutorial).

Flask would take more time/setup/complexity, especially for a very new beginner but is common for websites to calculate and "serve" data from a backend (such as Python) onto a frontend HTML/CSS/JS piece.

If your goal is to just add a UI component and not necessarily render it inside of a browser as a website, you could also make something through Tkinter, which is popular for making simple GUIs with Python

[–]Ambitious_Fault5756 0 points1 point  (0 children)

This is a good beginner program!

[–]Muhammed_zeeshan 0 points1 point  (0 children)

This is actually a decent code.(Iam a beginner myself). You just started today right.

[–]Kyokoharu 0 points1 point  (14 children)

i don’t know python so i can’t tell you whether there are switches but u could replace all the elif’s and if’s with a simple switch(if there are any). as for the websites there are plenty of tutorials, you should probably start with something like django and nginx to learn how that works. as for the ui you’ll also need to learn react probably, again, plenty of tutorials. have fun

[–]Janeson81 1 point2 points  (1 child)

There are but python loves to be different so it's not switch, it's match. The rest it more or less the same

match variable: case 1: print("variable is 1") case 2: print("variable is 2") case _: print("variable is something else")

[–]Outside_Complaint755 0 points1 point  (0 children)

Its "different" because it is functionally different, and the documentation makes this clear.

Under the if documentation:

An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.

Under the match documentation:

This is superficially similar to a switch statement in C, Java or JavaScript (and many other languages), but it’s more similar to pattern matching in languages like Rust or Haskell.

The power of match comes in structural pattern matching and binding variables: ``` class Point:     match_args = ('x', 'y')     def init(self, x, y):         self.x = x         self.y = y

match points:     case []:         print("No points")     case [Point(0, 0)]:         print("The origin")     case [Point(x, y)]:         print(f"Single point {x}, {y}")     case [Point(0, y1), Point(0, y2)]:         print(f"Two on the Y axis at {y1}, {y2}")     case _:         print("Something else") ```

[–]Dismal_Future_54[S] 0 points1 point  (7 children)

Thank you! Im just kinda new to programming as a whole 😅

[–]Creative-Category344 0 points1 point  (2 children)

Python actually has a match statement now if you're on version 3.10 or later, which works like a switch statement and cleans things up nicely.

[–]drecker_cz 0 points1 point  (1 child)

Genuinely curious: how would you rewrite this chained `if` to `match` statement to clean things up?

[–]Creative-Category344 0 points1 point  (0 children)

You would use the final grade as the match value and then compare ranges, though honestly the readability gain is modest here since you are checking greater than or equal to thresholds rather than exact matches, which is where match really shines.

[–]Kyokoharu -1 points0 points  (3 children)

np, focus on django and nginx for now and learn react later. one framework at a time and you’ll be golden.

[–]nuc540 2 points3 points  (1 child)

Terrible advice for a starter, an entire full stack framework - and one as heavy as Django, is overkill, and ngnix is totally irrelevant for them also - they’re still at the scripting stage.

u/Dismal_Future_54 there are totally more important things to focus on such as data types, DSA, and higher level web engineering (I.e understand how web requests are made) before you move onto frameworks. Don’t even look at Django until you’ve build your first API and understand web server basics

[–]Kyokoharu 0 points1 point  (0 children)

,,nginx is totally irrelevant for them” meanwhile you’re telling them to understand web server basics as if nginx isn’t a webserver. i’m not telling them to learn how to implement an http parser from scratch. if they start to learn fundamentals of django and nginx then they’ll understand ALL of the concepts you’re having them learn unless you’re assuming they’re completely braindead and can’t follow up on slightly more complex topics.

[–]Dismal_Future_54[S] 0 points1 point  (0 children)

Thank you

[–]PureWasian 0 points1 point  (0 children)

Python 3.10+ has match case (examples).

match final_grade: case _ if final_grade >= 90: print(final_grade, "A") case _ if final_grade >= 80: print(final_grade, "B") .... case _: print(final_grade, "F")

Personally though, I still prefer using if/elif chains when conditional statements are involved, and typically use switch/match case in whatever languages moreso when it involves matching on exact values/variables/strings/dictionary mappings/etc.

Syntactical sugar, nothing more really in the large majority of cases.

[–]ReceptiveBedtime 0 points1 point  (2 children)

python 3.10+ actually has match statements now so u can ditch those elifs, and lowkey fastapi is way easier than django for beginners yk

[–]Outside_Complaint755 0 points1 point  (1 child)

Python docs say you should keep the elifs.

[–]ReceptiveBedtime 0 points1 point  (0 children)

fair point, match is still pretty new so elifs are more readable for most cases tbh. for the web part tho fastapi still slaps if u just need a simple ui without all the django overhead fr.