рекурсия не работает во второй раз.- питон - PullRequest
0 голосов
/ 22 ноября 2018
def twothousand(amt):
    n=500
    div1=amt//n
    mod1=amt%n
    return (mod1,n,div1)


def fivehundred(amt):
    n=200
    div1=amt//n
    mod1=amt%n
    return (mod1,n,div1)


def calculate(amt):
    if amt <10:
        print("hi")

    elif amt>=200 and amt<500:
        mod1,n,div1=fivehundred(amt)
        return (mod1,n,div1)

        #the above return statement isn't returning anything. 
        #That is, now the program doesn't go to the main function 2nd time.

    elif amt>=500 and amt<2000:
        mod1,n,div1=twothousand(amt)
        return (mod1,n,div1)


def main1():
    amt=int(input("Enter the amount: "))
    mod1,n,div1=calculate(amt)
    print (mod1,n,div1)
    #The above print function executes only once.
    if mod1!=0:
        amt=mod1
        calculate(amt)


if __name__=="__main__":
    main1()

ВЫХОД:

Enter the amount: 1700
200 500 3

ОЖИДАЕМЫЙ ВЫХОД:

Enter the amount: 1700
200 500 3
0 200 1

Я не могу выполнить инструкцию возврата послеВызов функции calc () происходит 2 раза, как написано в комментариях.Я не получаю второй вывод.Новичок в Python, пожалуйста, помогите.

Извините, что не обновил логику ранее.Логика такова:

Когда пользователь запрашивает сумму 1700, ему может быть предоставлена ​​только эта сумма, используя 500 и 200 валют.Итак, 1-й выход - 200 500 3;то есть 3 числа из 500 валют .. и оставшиеся 200. Я хочу вызвать функцию вычисления до значения mod1 == 0.

1 Ответ

0 голосов
/ 22 ноября 2018

Ваша функция main () должна выглядеть так:

def main1():
    amt=int(input("Enter the amount: "))
    mod1,n,div1=calculate(amt)
    print (mod1,n,div1)
    #The above print function executes only once.
    if mod1!=0:
        amt=mod1
        mod1,n,div1 = calculate(amt)
        print (mod1,n,div1)
...