需要询问用户是否要再试一次(Python)

时间:2011-10-14 17:48:56

标签: python

#Ask the user what option he wants        
mode = input("Would you like to count Vowel's or Consonant's ? (Vowel or Consonant): ")
mode = mode.strip()
mode = mode.lower()

# Tell the user the input he entered wasn't valid        
while mode != 'consonant' and mode != 'vowel':
        mode = input("That's not correct. Would you like to count Vowel's or Consonant's ? (Vowel or Consonant): ")
#get the word from the user
word = input("Please enter your Word: ")
vowel_count = 0
consonant_count = 0
for letter in word:
    if letter in 'aeiouAEIOU':
        vowel_count += 1

for letter in word:
    if letter in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
        consonant_count += 1

if mode == "consonant":
        print(word,"contains", consonant_count, "consonant's")

if mode == "vowel":
        print(word,"contains", vowel_count, "vowel's")
  1. 程序启动并询问用户是否要计算元音或辅音,将其存储为“模式”。如果用户提供“辅音”或“元音”以外的输入,程序会将此解释为错误并重新请求输入。

  2. 程序要求一个字。

  3. 根据模式,计算辅音或元音的数量并报告给用户。

  4. 程序询问是否有其他单词可用。如果是,则重复步骤2到4,否则继续执行步骤5.

  5. 根据模式,向用户报告每个单词的平均元音或每个单词的平均辅音。

  6. 我坚持第4步,我不知道如何向对方要求另一个字并重复相同的过程

1 个答案:

答案 0 :(得分:2)

while c:
    do_stuff()
    c = raw_input('Do you want to contine y/n')
    if c.lower().startswith('y'):
        c = True
    else:
        c = False