[英]Not reading a vowel at the first of the loop
My program is suppose to get an alphabetic character from the user and determine if it is a vowel, consonant, space, or illegal character. 我的程序假设要从用户那里获取字母字符并确定它是元音,辅音,空格还是非法字符。
It says it is a consonant if I input 'a' when ran. 它说如果我在运行时输入“ a”是辅音。 User only inputs one character.
用户仅输入一个字符。 The space part of the loop works, but everything else is a consonant when I enter it.
循环的空格部分有效,但是当我输入它时,其他所有内容都是辅音。
myvar = raw_input("Enter an alpabetic character: ")
if myvar == ("a", "e", "i", "o", "u"):
print "Your character", myvar, "is a vowel"
elif myvar == " ":
print "Your character", myvar, "is a space"
elif myvar != "a, e, i, o, u, ,":
print "Your character", myvar, "is a consonant"
else:
print "Ooops! I think you've messed up"
Your comparisons are incorrect. 您的比较不正确。
myvar == ("a", "e", "i", "o", "u")
This compares myvar
to the tuple ("a", "e", "i", "o", "u")
. 这将
myvar
与元组("a", "e", "i", "o", "u")
。 I think you want something more like 我想你想要更多类似的东西
if myvar in ("a", "e", "i", "o", "u"):
This will check if myvar
appears in the tuple containing your vowels. 这将检查
myvar
出现在包含元音的元组中。
Using this methodology, you can adjust your consonant check to read: 使用这种方法,您可以将辅音检查调整为:
if myvar not in ("a", "e", "i", "o", "u"):
As you currently have it, you are checking if myvar
is equal to the string "a, e, i, o, u, ,"
目前,您正在检查
myvar
是否等于字符串"a, e, i, o, u, ,"
An edit: You could also convert your tuples to lists ["a", "e", "i", "o", "u"]
and keep the same checks. 编辑:您还可以将元组转换为列表
["a", "e", "i", "o", "u"]
并保持相同的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.