Добавление цикла while вводит вычисления пользователя в список.python3 - PullRequest
0 голосов
/ 29 марта 2019

Я использую python3, и у меня есть этот код, где я прошу пользователя для трех вводов. Затем я выполняю расчет по ним. Я хотел бы продолжать добавлять результаты расчетов в список. Как это сделать?

...

if choice == '1': #my fist input x
    while True:
        x = int(input('Please insert the number of things you consider yourself a fan of'))
        if x > 0:
            break
        else:
             print('Please insert a number higher than 0')

elif choice == '2': #my second input y
    while True:
        y = int(input('Please insert the number of hobbies you like to do every month'))
        if y % 4 == 0:
            break
        else:
            print('Please insert a valid number')

elif choice == '3': #my third input z
    while True:
        z = int(input('Please insert the number of sports you like'))
        if z > 0:
            break
        else:
            print('Please insert a number higher than 0')

elif choice == '4': #the calculation part
    import math
    def square_root():
        c=(42 * y ** 2)/(z + 1)
        nerd_score=int(x*math.sqrt(c))
        return nerd_score
    print('your nerd score is', square_root())

Я хочу, чтобы цикл продолжался, и каждый результат добавлялся в список. пока пользователь не выйдет из цикла.

1 Ответ

0 голосов
/ 29 марта 2019

В моем понимании есть две проблемы, которые вы хотите решить:

  1. цикл продолжается, если пользователь хочет выйти из цикла
  2. добавить каждый результат в список

Пример кода здесь:

def loop_calculation(choice):
    # record results in list
    results = []

    # loop keep going, util user input choice 'x' which means exit
    while choice != 'x':
        if choice == '1':  # my fist input x
            while True:
                x = int(input('Please insert the number of things you consider yourself a fan of'))
                if x > 0:
                    break
                else:
                    print('Please insert a number higher than 0')

        elif choice == '2':  # my second input y
            while True:
                y = int(input('Please insert the number of hobbies you like to do every month'))
                if y % 4 == 0:
                    break
                else:
                    print('Please insert a valid number')

        elif choice == '3':  # my third input z
            while True:
                z = int(input('Please insert the number of sports you like'))
                if z > 0:
                    break
                else:
                    print('Please insert a number higher than 0')

        elif choice == '4':  # the calculation part
            import math

            def square_root():
                c = (42 * y ** 2) / (z + 1)
                nerd_score = int(x * math.sqrt(c))
                return nerd_score

            result = square_root()
            print('your nerd score is', result)

            # add each result to list
            results.append(result)

    return results

Надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...