簡體   English   中英

我只想從我的用戶輸入中打印輔音

[英]i want to print only consonant from my user input

顯示用戶輸入的奇數字母將是大寫的,並且該字母也將使用類方法顯示大寫的輔音

我已經嘗試過大寫轉換,但結果需要以輔音顯示

c = input("Enter: ")
word = list(c)
for i, x in enumerate(word):
   if i % 2:
       word[i] = x.upper()
print("".join(word))
for i in word:
    if i.isupper():
        print(i,end='')

結果看起來是謊言

輸入:我是黑客
IAAKR(使用大寫方法)
KR(只打印輔音)

如果你只想要輔音而不是元音,那么有一個小修正:

vowel_list = ['A', 'E', 'I', 'O', 'U']
for i in word:
    if i.isupper():
        if i not in vowel_list:
            print(i,end='')

嘗試這個:

c = input("Enter: ")
word = list(c)
vowels = ['A', 'E', 'I', 'O', 'U']

upper = []
consonants = []

for i in range(0, len(word), 2):
    if word[i].isalpha():
        upper.append(word[i].upper())
        if word[i].upper() not in vowels:
            consonants.append(word[i].upper())

print(' '.join(upper))
print(' '.join(consonants))

這是你想要的嗎 ?

user_input = "Some sample input to check"

word_list = user_input.split(" ")

vowels = ['a', 'e', 'i', 'o', 'u']

for position, word in enumerate(word_list):
    if position % 2 == 0:
        word = word.upper()
        print("Word ",word)
        for letter in word:
            if letter.lower() not in vowels:
                print("Upper Case Consonant ",letter.upper())

Word  SOME
Upper Case Consonant  S
Upper Case Consonant  M
Word  INPUT
Upper Case Consonant  N
Upper Case Consonant  P
Upper Case Consonant  T
Word  CHECK
Upper Case Consonant  C
Upper Case Consonant  H
Upper Case Consonant  C
Upper Case Consonant  K

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM