Python - вспыхнуть первым, пока l oop? - PullRequest
1 голос
/ 24 января 2020

Хорошо, у меня есть какое-то время l oop на основе ввода здесь -

username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
while(len(username) > 0):
    counter = 0
    for c in username:
        if(c == 'a' or c == 'e' or c == 'i' or c == 'u' or c == 'o'):
            counter+=1
    ending = 'vowels' if counter > 1 else 'vowel'
    print(f"word has {counter} {ending}!")
    break

После печати количества гласных мне нужно всплыть и запросить новое имя пользователя. Но прерывать результаты при выходе из программы и продолжать бесконечно повторять последний отпечаток.

Как мне вернуться к первому вводу?

Ответы [ 4 ]

1 голос
/ 24 января 2020

Включите первый вход в l oop:

username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
while(len(username) > 0):
    counter = 0
    for c in username:
        if(c == 'a' or c == 'e' or c == 'i' or c == 'u' or c == 'o'):
            counter+=1
    ending = 'vowels' if counter > 1 else 'vowel'
    print(f"word has {counter} {ending}!")
    username = str(input('whats your favorite word? (enter to quit)')).strip().lower()

Программа завершится, когда вы не дадите никакого ввода (нажмите ввод непосредственно)

0 голосов
/ 24 января 2020

Я бы использовал if и break внутри l oop:

while True:
    username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
    if not username:  # check if username is an empty string
        break
    # calculations and print are here
0 голосов
/ 24 января 2020

Вам нужно получить еще один пользовательский ввод внутри l oop:

username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
while(len(username) > 0):
    counter = 0
    for c in username:
        if(c == 'a' or c == 'e' or c == 'i' or c == 'u' or c == 'o'):
            counter+=1
    ending = 'vowels' if counter > 1 else 'vowel'
    print(f"word has {counter} {ending}!")
    username = str(input('whats your favorite word? (enter to quit)')).strip().lower() # Here!
0 голосов
/ 24 января 2020

Если вы хотите повторить получение имен пользователей, то это утверждение должно быть в пределах al oop. Один стандартный способ - повторить этот код внизу вашего l oop.

username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
while(len(username) > 0):
    counter = 0
    for c in username:
        if(c == 'a' or c == 'e' or c == 'i' or c == 'u' or c == 'o'):
            counter+=1
    ending = 'vowels' if counter > 1 else 'vowel'
    print(f"word has {counter} {ending}!")
    # Get another username
    username = str(input('whats your favorite word? (enter to quit)')).strip().lower()
...