TransWikia.com

Python won't run my project- anyone want to check over my code?

Stack Overflow Asked on December 3, 2021

Starting on my first project, a calculator which solves different math problems. At this stage it only does Pythagora’s theorem (it has options to calculate the hypotenuse or another side). There is also an option to convert temperatures, though the actual code isn’t implemented yet.
I try to run the code with the latest version of python and it does that thing where it opens for a split second and then closes. I know this means something in the code is wrong. Because I’m new to this, I get things wrong a lot and I’m having trouble finding my mistakes.
This is my code if anyone wants to read it. Feedback is appreciated and I’m happy to answer any questions.

option = input("1. Pythagora's Theorem 2. Tempurature Conversions")
option = int(option)
if option == 1:
 op = input("1. Calculate the length of the hypotenuse 2. Calculate the length of another side")
 op = int(op)
  if op == 1:
   a = input("Enter the length of side a: ")
   b = input("Enter the length of side b: ")
   a = int(a)
   b = int(b)
   c = a*a + b*b
   print(c)
   print ("Answer is in surd form.")
  if option == 2:
   a = input("Enter the length of side a: ")
   hyp = input("Enter the length of the hypotenuse: ")
   a = int(a)
   hyp = int(hyp)
   b = hyp*hyp - a*a
   print(b)
   print("Answer is in surd form.")
  else:
   print("That is not a valid option. Enter a valid option number.")
else: ("That is not a valid option. Eneter a valid option number.")

Edit: It is fixed, problem was a missing ")" and probably my weird indentation. It all works fine now, thanks for all the help, it makes learning this much easier!

3 Answers

The python interpreter should actually give you feedback. Run python directly from a terminal so that your program doesn't close.

Here's a fixed version of your code which works:

option = input("1. Pythagora's Theorem 2. Tempurature Conversions")
option = int(option)
if option == 1:
    op = input("1. Calculate the length of the hypotenuse 2. Calculate the length of another side")
    op = int(op)
    if op == 1:
        a = input("Enter the length of side a: ")
        b = input("Enter the length of side b: ")
        a = int(a)
        b = int(b)
        c = a*a + b*b
        print(c)
        print ("Answer is in surd form.")
    elif op == 2:
        a = input("Enter the length of side a: ")
        hyp = input("Enter the length of the hypotenuse: ")
        a = int(a)
        hyp = int(hyp)
        b = hyp*hyp - a*a
        print(b)
        print("Answer is in surd form.")
    else:
        print("That is not a valid option number.")

Please note that most programmers use 4 spaces to indent code.

Answered by Lars Blumberg on December 3, 2021

You havent followed proper indentations, and you have made one logical mistake

option = input("1. Pythagora's Theorem 2. Tempurature Conversions > ")
option = int(option)
if option == 1:
    op = input("1. Calculate the length of the hypotenuse 2. Calculate the length of another side > ")
    op = int(op)
    if op == 1:
        a = input("Enter the length of side a: ")
        b = input("Enter the length of side b: ")
        a = int(a)
        b = int(b)
        c = a*a + b*b
        print(c)
        print ("Answer is in surd form.")
    elif op == 2:
        a = input("Enter the length of side a: ")
        hyp = input("Enter the length of the hypotenuse: ")
        a = int(a)
        hyp = int(hyp)
        b = hyp*hyp - a*a
        print(b)
        print("Answer is in surd form.")
    else:
        print("That is not a valid option. Enter a valid option number.")
elif option == 2:
    print("You havent programmed it")
else:
    print("That is not a valid option. Eneter a valid option number.")

Answered by Seyon Seyon on December 3, 2021

You're missing a ")" on line

a = input("Enter the length of side a: "

Answered by Xascoria Dung on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP