как: в то время как True в функции python 3 - PullRequest
0 голосов
/ 23 апреля 2020

Когда я использую этот код, все в порядке, но как поместить его в функцию?

Я не могу разорвать часть, когда внутри функции, всегда повторяю сообщение из ввода.

while True:
    country = input("Enter country of which you want to check pictures HR, RS, RO:  ").upper()
    if country == str("HR"): break
    if country == str("RO"): break
    if country == str("RS"):
        break
    else:
        print("Please enter HR or RO or RS: " + "you wrote: " + country)

Ответы [ 2 ]

0 голосов
/ 23 апреля 2020

Это рабочая комбинация для оператора if, но в противном случае она печатается многократно, если неправильно введена строка

def test(cot):
    while True:
        country = cot
        if country in ['HR', 'RO', 'RS']:
             return country
        else:
            print("Please enter HR or RO or RS: " + "you wrote: " + country)


test1 = test(input("Enter country of which you want to check pictures HR, RS, RO:  ").upper())
0 голосов
/ 23 апреля 2020

Вот решение:

def Function(cot):
    while True:
        country = cot
        if country == str("HR"): return print("HR")
        if country == str("RO"): return print("RO")
        if country == str("RS"):
            return print("RS")
        else:
            print("Please enter HR or RO or RS: " + "you wrote: " + country)

Вызов функции

Function(input("Enter country of which you want to check pictures HR, RS, RO:  ").upper())


...