首页 > 解决方案 > Python 不会运行我的项目——有人想检查我的代码吗?

问题描述

从我的第一个项目开始,一个解决不同数学问题的计算器。在这个阶段,它只做毕达哥拉定理(它可以选择计算斜边或另一边)。还有一个转换温度的选项,虽然实际的代码还没有实现。我尝试使用最新版本的 python 运行代码,它会在瞬间打开然后关闭。我知道这意味着代码中的某些内容是错误的。因为我是新手,所以我经常出错,而且我很难找到自己的错误。如果有人想阅读,这是我的代码。感谢您的反馈,我很乐意回答任何问题。

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.")

编辑:它是固定的,问题是缺少“)”,可能是我奇怪的缩进。现在一切正常,感谢所有帮助,它使学习变得更加容易!

标签: pythonproject

解决方案


python 解释器实际上应该给你反馈。直接从终端运行 python,这样你的程序就不会关闭。

这是您的代码的固定版本,它可以工作:

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.")

请注意,大多数程序员使用 4 个空格来缩进代码。


推荐阅读