gpt4 book ai didi

python - 输入 python 中的引号

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:54 25 4
gpt4 key购买 nike

使用 Python3 时,做一些简单的事情

x=input("Enter your name: ")

print (x)

要运行它,用户必须输入他们的名字“Steve”而不仅仅是 Steve。

有没有办法绕过输入引号?

最佳答案

我想你错了。在 Python 3 中,您不需要需要引号:

localhost-2:~ $ python3.3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("Enter your name:")
Enter your name:Steve
>>> x
'Steve'

你会在过去的 Python 2 时代,因为 input 基本上是 eval 你给它的东西:

>>> x = input("Enter your name:")
Enter your name:Steve
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Steve' is not defined
>>> x = input("Enter your name:")
Enter your name:"Steve"
>>> x
'Steve'

因此您将改用 raw_input:

>>> x = raw_input("Enter your name:")
Enter your name:Steve
>>> x
'Steve'

但是在 Python 3 中,inputraw_input 曾经的样子,因此不需要引号。

关于python - 输入 python 中的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14372852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com