python input() EOF 错误和 int 类型的返回值? [英] python input() EOF error and return value of type int?

查看:41
本文介绍了python input() EOF 错误和 int 类型的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这 2 个短代码,我有 2 个问题.

I have 2 questions on these 2 short codes.

1.

name = input('what: ')
print(name)

输出

what: 641u
Traceback (most recent call last):
  File "/Users/vuthynun/PycharmProjects/untitled1/h/__init__.py", line 1, in <module>
    name = input('what: ')
  File "<string>", line 1
    641u
       ^
SyntaxError: unexpected EOF while parsing

Process finished with exit code 1

为什么它给我这个错误?

Why does it give me this error?

2.

name = input('what: ')

print(type(name))

输出:

what: 23
<type 'int'>

Process finished with exit code 0

为什么名字类型是int?我以为所有输入都转换为字符串.

Why is name type int? I thought all input are converted to string.

请帮助我理解这一点.害死我了.

Please please please help me understand this. It's killing me.

推荐答案

在 python 2 中使用 raw_input 而不是 input.这就是你获得字符串的方式.否则python会评估你给它的东西,这会给你意想不到的行为.

Use raw_input instead of input in python 2. That's how you get strings. Otherwise python will evaluate what you give it, which is giving you your unexpected behaviour.

这篇关于python input() EOF 错误和 int 类型的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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