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

0 投票
1 回答
2622 浏览
提问于 2025-04-17 04:20
  1. #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")
    

    程序开始后,会询问用户想要计算元音还是辅音,这个选择会被存储为一个“模式”。如果用户输入的不是“辅音”或“元音”,程序会把这个当作错误,并重新询问用户。

  2. 程序会要求用户输入一个单词。

  3. 根据之前选择的模式,程序会计算并告诉用户有多少个辅音或元音。

  4. 程序会询问用户是否还有其他单词。如果有,就重复步骤2到4;如果没有,就继续到步骤5。

  5. 根据之前选择的模式,程序会告诉用户每个单词的平均元音数或平均辅音数。

我在第4步卡住了,不知道怎么询问用户是否还有其他单词,并重复相同的过程。

1 个回答

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

撰写回答