Python 不会 运行 我的项目 - 有人想检查我的代码吗?
Python won't run my project- anyone want to check over my code?
从我的第一个项目开始,一个解决不同数学问题的计算器。在这个阶段它只做毕达哥拉定理(它有计算斜边或另一边的选项)。还有一个转换温度的选项,但实际代码尚未实现。
我尝试 运行 使用最新版本 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.")
编辑:已修复,问题是缺少“)”,可能是我奇怪的缩进。现在一切正常,感谢所有帮助,这让学习变得更加容易!
您在行中少了一个“)”
a = input("Enter the length of side a: "
你没有遵循正确的缩进,你犯了一个逻辑错误
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.")
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 个空格来缩进代码。
从我的第一个项目开始,一个解决不同数学问题的计算器。在这个阶段它只做毕达哥拉定理(它有计算斜边或另一边的选项)。还有一个转换温度的选项,但实际代码尚未实现。 我尝试 运行 使用最新版本 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.")
编辑:已修复,问题是缺少“)”,可能是我奇怪的缩进。现在一切正常,感谢所有帮助,这让学习变得更加容易!
您在行中少了一个“)”
a = input("Enter the length of side a: "
你没有遵循正确的缩进,你犯了一个逻辑错误
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.")
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 个空格来缩进代码。