python - 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.")
编辑:它是固定的,问题是缺少“)”,可能是我奇怪的缩进。现在一切正常,感谢所有帮助,它使学习变得更加容易!
解决方案
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 个空格来缩进代码。
推荐阅读
- android - Abi 过滤器 - 被一个或多个具有更高版本代码的 APK 遮蔽
- python - 无法在同一目录和包中导入模块
- java - “java.io.FileNotFoundException:没有文件匹配规范”虽然文件已成功写入
- linux - 我的 Vncserver 设置有什么问题?
- java - 定义单例与在接口中声明
- ios - 如何修复 Xcode 10 中的小部件库,因为它在选择一个小部件 ios 后隐藏起来
- javascript - 缺少)在脚本管理器的参数列表错误之后
- ios - 如何删除左 UIBarButtonItem 的填充?
- javascript - 如何将原始数据主体添加到 axios 请求?
- jetty - 码头空闲超时异常