【问题标题】:Input quote, output in caps, lower case and reverse [duplicate]输入引号,大写输出,小写和反向[重复]
【发布时间】:2015-12-10 21:23:19
【问题描述】:

我正在尝试在 Python 3 (IDLE) 中编写一个程序,该程序允许用户输入引号并以大写、小写和反向输出。我试过这个:

quote = input("Enter your quote here: ")
print(quote.upper())
print(quote.lower())
print(quote.reverse())

...但是当我测试它时我得到的只是这个错误文本:

Enter your quote here: You are always unique.

Traceback (most recent call last):
  File "C:\Users\shobha m nair\Documents\Quote Conversion.py", line 2, in <module>
    quote = input("Enter your quote here: ")
  File "<string>", line 1
    You are always unique.
          ^
SyntaxError: invalid syntax

【问题讨论】:

  • 您正在使用 Python 2,其中 input 在结果上运行 eval
  • 您正在寻找raw_input

标签: python reverse uppercase lowercase


【解决方案1】:

您可能正在使用 Python 2.x,这就是您遇到意外行为的原因。以下代码在 Python 3.5 上运行良好:

quote = input("Enter your quote here: ")
print(quote.upper())
print(quote.lower())
print(quote[::-1])

字符串没有“反向”方法。

【讨论】:

    【解决方案2】:

    这对我有用:

    quote = input("Enter your quote here: ") print(quote.upper()) print(quote.lower()) print(quote[::-1])

    最后一个是扩展拼接运算符。 AFAIK 在 Python 3 中没有反向方法。

    【讨论】:

    • 请注意,这在 Python 2 中不起作用,并且会导致问题中描述的问题。
    • 是的,但他不是特别说他使用的是 Python 3 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2013-03-29
    • 1970-01-01
    相关资源
    最近更新 更多