python SyntaxError:解析时出现意外的EOF [英] python SyntaxError: unexpected EOF while parsing

查看:1161
本文介绍了python SyntaxError:解析时出现意外的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码

m, b = eval(input())

目标是输入一堆用逗号分隔的值,然后让python将元组解包到变量中

the aim is to have a whole bunch of comma separated values inputted and then have python unpack the tuple into the variables

但是当我运行时出现此错误

but when i run i get this error

    x, y = eval(input())
  File "<string>", line 1
    1,2

           ^
SyntaxError: unexpected EOF while parsing

我做错了什么?

im使用python 3

im using python 3

推荐答案

对于此类事情,请勿使用 eval 。不可能以用户无法破坏(错误或故意)破坏的方式来编写它。改为执行以下操作:

You should not use eval for things like this. It will be impossible to write it in a way such that the user can't break it (by mistake or on purpose). Do something like this instead:

data = input()
m, b = (int(var) for var in data.split(","))

这篇关于python SyntaxError:解析时出现意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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