我正在使用 Pycharm 3.4.1 和 Python 版本 3。两天来,我的基本/简单代码无法工作。Pycharm 表示代码执行时错误为 0。但最终没有打印或结果。
有人可以检查代码吗?有没有人经历过同样的事情?
代码1:
#Welcome to basic calculator, let's start..
a = int(input("Enter your first number: "))
b = int(input("Enter your second number: "))
type = str(input("Please choose your calculation type: +, -, *, / "))
if type == "+":
print("The result is: ", a + b)
elif type == "-":
print("The result is: ", a - b)
elif type == "*":
print("The result is: ", a * b)
elif type == "/":
print("The result is: ", a / b)
else:
print("The command you entered is not valid")
Run Code Online (Sandbox Code Playgroud)
代码2:
class Enemy:
life = 3
def attack(self):
print('attack!')
self.life -= 1
def check(self):
if self.life <= 0:
print("I am dead")
else:
print(str(self.life) + " life left")
enemy1 = Enemy()
enemy2 = Enemy()
# each object is independent of one another, they don't share variables
enemy1.attack()
enemy1.attack()
enemy1.check()
enemy2.check()
Run Code Online (Sandbox Code Playgroud)
小智 1
我也遇到了同样的问题,而且我也是 python 和 pycharm 的菜鸟。我按照JetBrains 调试教程进行操作,他们的示例脚本 ThreadSample.py 跳入调试器并按照预期的方式完成了所有操作。但后来我花了几天时间尝试调试我自己的 python 脚本,甚至让它运行!(在 IDLE 中完美运行。)最终对我有用的是创建一个全新的项目,仅将该脚本复制到其中,现在 Pycharm 可以用它完成它应该做的一切。我的猜测是,Pycharm 对文件系统进行了如此多的微观管理,当它在一个包含一堆与 python 或 Pycharm 没有任何关系的文件的文件夹中发现我的原始脚本时,它变得非常混乱。不管怎样,这对我有用。