是否有一个程序只返回单词python中的常量

时间:2016-01-10 13:02:47

标签: python python-3.x

我想写一个程序,询问用户一个单词,只返回常量。但是我坚持使用这个程序,我希望这个程序可以做我上面提到的,但它所做的就是逐行返回这个单词。

original = input('Enter a word:')
word = original.lower()
first = word[0]
second = word[1]
third = word[2]
fourth = word[3]
fifth=word[4]


if first == "a" or "e" or "i" or "u":
    print (first)
else:
    print ("")
if second == "a" or "e" or "i" or "u":
    print (second)
else:
    print ("")
if third == "a" or "e" or "i" or "u":
    print (third)
else:
    print ("")
if fourth == "a" or "e" or "i" or "u":
    print (fourth)
else:
    print ("")
if fifth == "a" or "e" or "i" or "u":
    print (fifth)
else:
    print ("")

2 个答案:

答案 0 :(得分:1)

从单词中删除所有元音并打印出来:

original = input('Enter a word:')
print("".join(x for x in original if x.lower() not in "aeiou"))

答案 1 :(得分:0)

优先权是错误的。 "或"优先级高于" ==" - 见https://docs.python.org/2/reference/expressions.html#operator-precedence

所以这一行:

if first == "a" or "e" or "i" or "u":

与:

相同
if (first == "a") or ("e") or ("i") or ("u"):

我怀疑它不是你想要的。以下可能会更好:

if first in "aeiu":

但是也要注意这是区分大小写的......