语法错误无效,我该怎么办?

时间:2013-09-05 15:02:50

标签: python python-3.x

python新手。似乎无法让这个工作......

print = 'Press "U" and "Enter" for upper case.'
print = 'Press "L" and "Enter" for lower case.'
print = 'Press "C" and "Enter" for Capitalisation.'

letter = input("Please type a letter and press enter: ")
if letter == u: print '"THE MOST PROFOUND TECHNOLOGIES ARE THOSE THAT DISAPPEAR: THEY     WEAVE THEMSELVES INTO FABRIC OF EVERYDAY LIFE UNTIL ARE INDISTINGUISHABLE FROM IT" [MARK     WEISER, THE COMPUTER FOR THE 21ST CENTURY, SCIENTIFIC AMERICAN, SEPT. 1991]'
if letter == l: print '"the most profound technologies are those that disappear: they     weave themselves into fabric of everyday life until are indistinguishable from it" [mark weiser, the computer for the 21st century, scientific american, sept. 1991]'
if letter == c: print '"The most profound technologies are those that disappear: they weave themselves into fabric of everyday life until are indistinguishable from it" [Mark Weiser, The Computer for the 21st Century, Scientific American, Sept. 1991]'

我如何改进程序,以便用户可以替换一个单词 用另一个词。

4 个答案:

答案 0 :(得分:0)

应该说

print('Press "U" and "Enter" for upper case.')
print('Press "L" and "Enter" for lower case.')
print('Press "C" and "Enter" for Capitalisation.')

我建议使用pythons string.replace函数替换字符串:)

http://docs.python.org/2/library/string.html

答案 1 :(得分:0)

print('Press "U" and "Enter" for upper case.')

答案 2 :(得分:0)

你也应该在你的用户输入上使用像upper()这样的函数来考虑大写和小写的值:

if letter.upper() == U ........

答案 3 :(得分:0)

也许像

formats = {"U": {"text": "upper case", "func": str.upper},
           "L": {"text": "lower case", "func": str.lower},
           "C": {"text": "Capitalization", "func": lambda x: x}
          }

data = '"The most profound technologies are those that disappear: they weave themselves into fabric of everyday life until are indistinguishable from it" [Mark Weiser, The Computer for the 21st Century, Scientific American, Sept. 1991]'

for c in formats:
    print('Press "{}" and "Enter" for {}.'.format(c, formats[c]['text']))

letter = input("Please type a letter and press enter: ")
if letter in formats:
    print(formats[letter]['func'](data))