r/learnpython • • 7h ago

Need help with a calculator project

4 Upvotes

new to python, Im making a calculator but when i try to make the code be able to handle a word without crashing it just doesnt work, it just goes and ask for the operator without restarting the loop, tried ai to fix it and explain it to me but didnt do nothing.

num1 = None
num2 = None
result = None


while num1 == None or num2 == None:
    try:
        num1 = float(input("Enter first number: "))
    
    except ValueError:
        print("must enter a valid number")
        num1 = None
        continue


    try:
        num2 = float(input("enter second number"))


    except ValueError:
        print("must enter a valid number")
        num2 = None
        continue
    operator = input("Enter operation (+, -, *, /)")


    if operator == "+":
        result = (num1 + num2)


    elif operator == "-":
        result = (num1 - num2)


    elif operator == "*":
        result = (num1 * num2)


    elif operator == "/" and num2 == 0:
        result = ("cant divide by zero")


    elif operator == "/" and num2 != 0:
        result = (num1 / num2)


    else:
        result = ("Invalid operator")


    if result == float or int:
        print(result)
        num1 = None
        num2 = Nonenum1 = None
num2 = None
result = None


while num1 == None or num2 == None:
    try:
        num1 = float(input("Enter first number: "))
    
    except ValueError:
        print("must enter a valid number")
        num1 = None
        continue


    try:
        num2 = float(input("enter second number"))


    except ValueError:
        print("must enter a valid number")
        num2 = None
        continue
    operator = input("Enter operation (+, -, *, /)")


    if operator == "+":
        result = (num1 + num2)


    elif operator == "-":
        result = (num1 - num2)


    elif operator == "*":
        result = (num1 * num2)


    elif operator == "/" and num2 == 0:
        result = ("cant divide by zero")


    elif operator == "/" and num2 != 0:
        result = (num1 / num2)


    else:
        result = ("Invalid operator")


    if result == float or int:
        print(result)
        num1 = None
        num2 = None

r/learnpython • • 52m ago

2 weeks in learning python from Scratch

• Upvotes

Thanks to whoever in this sub suggested CS50 Harvard YouTube & Automate the boring stuff book!

I'm 2 weeks in and feel like I'm in "good" place so far!

I'm also using AI to create similar problem sets to CS50's so I can practice more to ensure full understanding of the lecture!


r/learnpython • • 15h ago

dynamic import module path hack in pytest

3 Upvotes

In order to validate (using pytest) a python module which is not in my path or relative folders, I want to hack the import path.

I'm wondering what alternatives there are to doing a PYTHONPATH hack. I could use https://docs.pytest.org/en/7.1.x/reference/reference.html#confval-pythonpath, but that applies to the entire session as I understand it. And I only want to change one suite, so I am writing a quick fixture that will roughly look like (hard-coded for simplicity) @pytest.fixture(scope="function") def modules_path(): old_path = sys.path extra_path= os.path.abspath("c:/otherpath/other") # replace constant with a func sys.path.insert(0, extra_path) yield extra_path sys.path = old_path I've always felt there was a better way to do import path hacks that nobody had ever told me about. Or is this @fixture approach not as evil as I assumed it was, and was merely biased by painful memories whenever I did use it. Alternatives?


r/learnpython • • 18h ago

Dsa in python

0 Upvotes

Hey everyone! I'm planning to learn Data Structures and Algorithms using Python.

Is it worth learning DSA with Python for placements, coding interviews, and future software engineering jobs? Or is it better to learn DSA using C++ or Java?

For those who have experience, which language would you recommend and why?


r/learnpython • • 5h ago

Python 3.8.10 Broken on vista

0 Upvotes

i tried install python in vista and it mid freeze in the instalation and i even use the repair tool in the installer but i keeped having the error "The Procedure entry point GetActiveProcessor could not be located in the dinamic link library KERNEL32.dll" and then a Python has stoped working, something weird here is why is it calling kernel32.dll? and i do not expet support since this is a outdated version but in windows 7 starter (same pc) it worked fine, i dont know if this is else becuse im using an modified version of vista (Vienna) or is else something to do with 3.8.10 in vista


r/learnpython • • 8h ago

requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

0 Upvotes
import requests


# Add ?format=j1 to get a proper JSON response
url = "https://wttr.in"
response = requests.get(url)
data = response.json()


# This structure matches the j1 JSON format returned by wttr.in
current_temp = data['current_condition'][0]['temp_C']
print(
f
"Current temperature: {current_temp}°C")

I keep getting this exact SAME error whenever I work with public API's. I save the new codes in the files.

r/learnpython • • 8h ago

Looking for a coding buddy

0 Upvotes

Hey everyone!

I’m looking for a coding buddy who’s also taking 100 Days of Code: The Complete Python Pro Bootcamp by Dr. Angela Yu on Udemy.

I’m just starting with Day 1 and would love to connect with someone taking the same course so we can learn together throughout this 100-day journey.

The goal is to keep each other motivated and accountable, share our progress, discuss what we’ve learned, help each other when we get stuck, and celebrate our little wins along the way.

It would be great to have someone who’s also a beginner, but anyone taking the same course is welcome! No pressure to be an expert — we’re here to learn and improve together.

If you’re taking the same course, feel free to comment or send me a message. Let’s keep each other motivated and complete these 100 days together!

Happy coding!


r/learnpython • • 19h ago

Best laptop to get

0 Upvotes

What are the best Windows laptops I can get for AI development, machine learning, and video editing? I’m looking for a laptop that can handle demanding tasks smoothly and is suitable for long-term use. Please recommend the best options based on performance, specifications, . I’d really appreciate your help!


r/learnpython • • 19h ago

Python language tutorial

0 Upvotes

Hey guys, i wanna learn Python from scratch. I dont have any prior knowledge about any coding language. Please tell me from where should i learn. Mainly for Computational modelling, SciPy,NumPy etc


r/learnpython • • 16h ago

What would be the best course of action if u were flagged for cheating?(PCEP)

0 Upvotes

For context: i started the pcep test while forgetting that it was still connected to a 2nd monitor and my cursor flew into the 2nd screen which got me flagged for cheating. What would be the best course of action right now for me?


r/learnpython • • 6h ago

I need some help

0 Upvotes

I want to learn python but all of the online tutorials just tell me how to make a certain game but i want to learn how it works so if anyone can help me with this please let me know


r/learnpython • • 23h ago

Can any of you rate my codewars question

0 Upvotes

It's one i've been thinking about for a while and i'd love to know all of your opinions about it! https://www.codewars.com/kata/6ad8699fccb2dbf6de41f760/python