r/PythonLearning • u/Extension-Cut-7589 • Aug 22 '25
Practicing what I learnt in Python
I have been learning Python on my own for the past few months using the book titled ‘Python Crash Course’, it’s a book I am really enjoying.
So I want to ask few questions as a beginner: Is this a good project as a beginner? Also how can I improve this or take it further? Any resources for me to do more practices as a beginner?
1
u/ProfessionalStuff467 Aug 23 '25
great work bro ; do you use pycharm ?
2
3
u/Mohit_Gupta_007 Aug 23 '25
I think if you are learner you use now thonny
1
u/ProfessionalStuff467 Aug 23 '25
NO I USE pycharm
2
u/Mohit_Gupta_007 Aug 25 '25
If you want know how to code works then I think you want to install thonny because it’s debugger so awesome once time you must try .
1
1
u/Kqyxzoj Aug 23 '25
A few points:
Since you don't have a finally statement in that try block, and continue on the exception, you don't really need that else. So you can put that print statement after the try-except block.
And since the area calculation will not trigger a ValueError, might as well put the area calculation outside of the try-except as well.
Should you have provided the code as text I would have copy/paste/edited it to show the code as described above. But since it is a screen "shot" and I am not going to re-type or OCR it, hopefully the above description is sufficient.
1
u/Extension-Cut-7589 Aug 24 '25
# Writing a program to calculate the area of a triangle. # Area of a triangle is 1/2* base * height while True: try: base = float(input('Enter the base of the Triangle: ')) height = float(input('Enter the height of the Triangle: ')) area = 0.5 * base * height except: ValueError print('Sorry Invalid, Please insert a number!\n') continue else: print(f'The area of the triangle is: {area}\n') print('Would you like to do another calculation?') another = input('Enter Y for yes and N for no: ') if another.lower() == 'y': continue else: break Here is the code.1
u/Kqyxzoj Aug 24 '25
Changes more or less as described:
while True: try: base = float(input('Enter the base of the Triangle: ')) height = float(input('Enter the height of the Triangle: ')) except ValueError: print('Sorry Invalid, Please insert a number!\n') continue area = 0.5 * base * height print(f'The area of the triangle is: {area}\n') print('Would you like to do another calculation?') another = input('Enter Y for yes and N for no: ') if another.upper() != 'Y': breakNote that your choice to put both
input()statements in the sametryblock means that when you enter a valid base and an invalid height ... you will have to re-enter the value for base as well.Also, changed the
exceptstatement to reflect your probable intent.
1
u/Medium_Human887 Aug 24 '25
The break is unnecessary and I would say it’s good practice to not get used to using it. Most times people don’t use it well. But looks good overall!
1
3
1
u/DataPastor Aug 23 '25
Copy your code and paste it to chatgpt and ask for code review. It will come up with plenty of useful suggestions.
For example you shouldn’t use the try … except block over multiple lines of code. You should wrap only single operations with it, and handle all the expected error types separately.
The except shouldn’t be used like this. You should name the exact error type after it, that is, except ValueError as ve: and then handle the errors separately. Each try… block can have multiple except clauses btw.
It is a good practice to use pylance, pylint, autopep8 etc. extensions while you are coding, they also give you great suggestions. Happy coding!
1
2
1
u/the_fish_king_sky Aug 24 '25
You should have this and know where it is https://docs.python.org/3/library/index.html
0
u/DevRetroGames Aug 23 '25
Hola, excelente, aquí te paso el mismo código, solo un poco más modular, espero que te pueda ayudar.
#!/usr/bin/env python3
import sys
import re
def _get_user_input(msg: str) -> str:
return input(msg)
def _calculate_area(base: float, height: float) -> float:
return 0.5 * base * height
def _get_continue() -> bool:
resp: list[str] = ["Y", "N"]
msg: str = """
Would you like to do another calculation?
Enter Y for yes or N for n:
"""
while True:
another = _get_user_input(msg).upper()
if another not in resp:
print("Input invalid.")
continue
return another == "Y"
def _is_positive_float(value: str) -> bool:
pattern = re.compile(r'^(?:\d+(?:\.\d+)?|\.\d+)$')
return bool(pattern.fullmatch(value))
def _get_data(msg: str) -> float:
while True:
user_input = _get_user_input(msg)
if _is_positive_float(user_input) and float(user_input) > 0:
return float(user_input)
print("Input invalid.")
def execute() -> None:
while True:
base: float = _get_data("Enter the base of the triangle: ")
height: float = _get_data("Enter the heght of the triangle: ")
area: float = _calculate_area(base, height)
print(f"The area of the triangle is {area}")
if not _get_continue():
break
def main() -> None:
try:
execute()
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
sys.exit(0)
if __name__ == "__main__":
main()Mucha suerte en tu camino.
1
u/Extension-Cut-7589 Aug 23 '25
Thanks!
1
u/sububi71 Aug 23 '25
Take note how much longer this version of the program is, and how it is a lot harder to read...
4
8
Aug 23 '25
[removed] — view removed comment
1
u/Extension-Cut-7589 Aug 23 '25
Could you please explain further, I don’t understand what you meant?
3
1
1
Aug 24 '25
[removed] — view removed comment
1
u/Extension-Cut-7589 Aug 24 '25
I started it with a book titled "Python Crash Course." It is a really good book and beginner-friendly too.
6
u/ProfessionalStuff467 Aug 23 '25
I am a beginner like you, but I have some things that help me, which is that I am learning the basics of Python, and if you want a site to learn, I advise you to use Sololearn. It is a site like Duolingo, but it is dedicated to important programming languages. Also, the most important advice is that you create several projects in which you apply what you have learned and expand your knowledge.