简体   繁体   English

代码在命令提示符下运行,但不在氢气上运行

[英]Code runs on command prompt but not on hydrogen

I'm just beginning with programming(python3), using the information available on Inte.net.我刚开始使用 Inte.net 上提供的信息进行编程 (python3)。 Right now I'm learning how to use try/except.现在我正在学习如何使用 try/except。 My problem is that the code I wrote runs fine in the command prompt of windows 10, but not in the shell(Atom/Hydrogen) where it throws an error(line 6, NameError) because I didn't defined the variable "fish", I know that usually happened the other way around but I just want to understand if I'm making a mistake.我的问题是我编写的代码在 windows 10 的命令提示符下运行良好,但在它抛出错误的 shell(Atom/Hydrogen) 中运行不正常(第 6 行,NameError),因为我没有定义变量“fish” ,我知道这通常是相反的,但我只想了解我是否犯了错误。 The code is as follows:代码如下:

>try:
>>    fish = int (input("please enter your age "))
>except:
>>    print("That's not a number ")
>>    exit(0)
>if fish <= 15:
>>    parentLicense = input ("have one of your parents have a license? (yes/no) ")
>>    if parentLicense == "yes":
>>>        print ("You can fish")
>>    else:
>>>        print("So sad, you or one of your parents need a license")

Hi Chiron and welcome to the community.嗨 Chiron,欢迎来到社区。 The reason you are getting an undefined error is because fish can be undifined under certain circumstances in the try statement.您收到未定义错误的原因是因为在 try 语句的某些情况下 fish 可能是未定义的。 You should use你应该使用

try:
    # try stuff
except ValueError:
    # do stuff when theres an error
else:
    # stuff if try stuff works

else is only called if no exception is raised. else 只有在没有异常发生时才会被调用。 I would avoid using bare except too as it can cause issues and its not good practice.我会避免使用 bare except ,因为它会导致问题并且不是好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM