python - 我可以浓缩这个吗?
问题描述
invalid_characters = ["1","2","3","4","5","6","7","8","9","0"]
vowels = ["a","e","i","o","u"]
letter = str(input("Enter a letter: "))
while letter in invalid_characters:
letter = input("You entered an invalid character. Please enter a letter: ")
if letter in vowels:
print("The letter is a vowel")
else:
print("The letter is a consonant")
解决方案
如果你的意思是你想让你的代码更短,试试这个:
vowels = "aeiou"
letter = str(input("Enter a letter: "))
while letter.isdigit():
letter = input("You entered an invalid character. Please enter a letter: ")
if letter in vowels:
print("The letter is a vowel")
else:
print("The letter is a consonant")
但是,请记住,如果您输入“$”或任何其他特殊符号,它会说它是一个辅音。
如果你想让它更快一点,就像让-弗朗索瓦·法布尔提到的那样,你可以使用一组来in
加快速度。
vowels = set("aeiou")
letter = str(input("Enter a letter: "))
while letter.isdigit():
letter = input("You entered an invalid character. Please enter a letter: ")
if letter in vowels:
print("The letter is a vowel")
else:
print("The letter is a consonant")
推荐阅读
- javascript - Atom Live Server 中的空数组。本地机器上的 Null
- html - 如何将屏幕分成3个面板并分别处理每个面板
- python - 如何更改 Pyomo 求解器界面
- java - 支持跨时区和夏令时更改的重复事件
- javascript - 获取嵌入对象标签的 HTML 页面的参数
- mysql - 按帐号显示每次注册的 MYSQL 中的最新行
- video - 使用 FFMPEG 输出多个 SDP 文件
- java - 使用场景生成器制作程序
- python - python marshmallow force field.int 只接受 int 而不是字符串
- apache - 厨师通过包装食谱编辑现有配置