Сбой функции печати из-за цикла for - PullRequest
0 голосов
/ 28 января 2019

Когда я закомментирую цикл for, все работает.Однако, как только он снова активен, IDLE выдает синтаксическую ошибку.Я попытался сделать это соответствующим образом, так как это было одним из предложений по другим вопросам, но это не сработало, независимо от того, сколько раз я нажимал ввод.

# futval.py
# A program to compute the value of an investment
# carried 10 years into the future

#begins main function of program

def main():

    #description of program 

    print("This program calculates the future value")
    print("of a 10-year investment.")

    #creates and assign three variables to user input

    principal = eval(input("Enter the initial principal: "))
    apr = eval(input("Enter the annual interest rate: "))
    compound = eval(input("How often is the interest compounded: "))

    #begins a 'for' loop that iterates 10 times indicative of 10 years

    for i in range(10):
        print (principal)
        principal = principal * (1 + (apr/compound)

    #prints out the final result of the above equation. 
    print("The value in 10 years is:",principal)

    #original program exited too fast and didn't allow user to see output
    #I added the following line so the user could see.

    print(input("Press enter to exit."))

main()

Ответы [ 3 ]

0 голосов
/ 28 января 2019

вы пропустили ) после (apr/compound)

for i in range(10):
        print (principal)
        principal = principal * (1 + (apr/compound))
0 голосов
/ 28 января 2019

В вашем коде отсутствует скобка.В противном случае он работает нормально.

ваш код:

principal = principal * (1 + (apr/compound)

должно быть: `

principal = principal * (1 + (apr/compound))`
0 голосов
/ 28 января 2019

Синтаксическая ошибка в том, что вам не хватает одной закрывающей скобки: Измените principal = principal * (1 + (apr/compound) на principal = principal * (1 + (apr/compound))

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

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