Проблема с while и if-else

До def test() вроде все норм, но непосредственно в функции def tes() запуталась и не хватает знаний. И ещё. Можно ли вообще не использовать while и обойтись только if-else?! Хотела воспользоваться только ими, но все остальные использовали while и я решила, что тоже надо вставить.

def greet(bot_name, birth_year):
    print('Hello! My name is ' + bot_name + '.')
    print('I was created in ' + birth_year + '.')


def remind_name():
    print('Please, remind me your name.')
    name = input()
    print('What a great name you have, ' + name + '!')


def guess_age():
    print('Let me guess your age.')
    print('Enter remainders of dividing your age by 3, 5 and 7.')

    rem3 = int(input())
    rem5 = int(input())
    rem7 = int(input())
    age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105

    print("Your age is " + str(age) + "; that's a good time to start programming!")


def count():
    print('Now I will prove to you that I can count to any number you want.')

    num = int(input())
    curr = 0
    while curr <= num:
        print(curr, '!')
        curr = curr + 1


def test():
    print("Let's test your programming knowledge.")
    # write your code here
    print("Why do we use methods?")
    print("1. To repeat a statement multiple times.")
    print("2. To decompose a program into several small subroutines.")
    print("3. To determine the execution time of a program.")
    print("4. To interrupt the execution of a program.")
    while true:
        answer = int(input())
        answer != 2
        print("Please, try again.")
    else:
        print('Completed, have a nice day!')


def end():
    print('Congratulations, have a nice day!')


greet('Aid', '2020')  # change it as you need
remind_name()
guess_age()
count()
# ...
end()

Ответы (1 шт):

Автор решения: Roman Konoval

Вероятно в этом куске кода:

    while true:
        answer = int(input())
        answer != 2
        print("Please, try again.")
    else:
        print('Completed, have a nice day!')

вы пропустили if. Предполагалось, я так думаю:

    while true:
        answer = int(input())
        if answer != 2:
            print("Please, try again.")
        else:
            print('Completed, have a nice day!')
            break
→ Ссылка