Python尝试除了其他无效的语法? [英] Python try except else invalid syntax?

查看:145
本文介绍了Python尝试除了其他无效的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在Python的IDLE中设置一个小脚本。 IDLE语法检查告诉我这段代码有语法错误:

So I am trying to set up a small script in Python's IDLE. The IDLE syntax check tells me this code has a syntax error:

from ftplib import FTP
import os
def ftpconnect(address, username, password):
    ftp_connection = 0
    ftp = FTP(address)
    try:
        ftp.login(username, password)
        print(ftp.getwelcome())
        if ftp.getwelcome() == '220 FTP Connected!':
            return 1
    else:
        return 0
print(ftpconnect('10.10.10.xxx', 'xxx', 'xxx'))

语法错误来自我试图摆脱try语句的任何地方,这里是else:行。我环顾四周,似乎我有正确的语法......有什么想法吗?

The syntax error comes anywhere that I try to get out of the "try" statement, here being the "else:" line. I've looked around and it seems like I have the right syntax...any thoughts?

谢谢!我正在运行Python 2,而不是3。

Thanks! I'm running Python 2, not 3.

推荐答案

好吧,我自己想出来了。问题不在于我的语法,而在于我的ftp尝试。说ftp = FTP(地址)的行是我需要放入try:语句的行,因为我没有尝试它,它无论如何都失败了。我之所以遇到语法错误,是因为我使用的是其他的,而不是除了。

Alright, I figured it out on my own. The problem was not with my syntax, it was with the my ftp attempt. The line that says "ftp = FTP(address)" was the one I needed to put inside a try: statement, and since I was not "try"ing it, it failed anyway. The reason I was getting a syntax error I believe is because I was using else, rather than except.

这篇关于Python尝试除了其他无效的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆