【问题标题】:I am getting a Name error during an If statement在 If 语句中出现名称错误
【发布时间】:2022-11-10 22:30:51
【问题描述】:

将绑定输入程序时出现名称错误未定义错误。

如果有人能阐明我哪里出错了,那就太好了。

import math

greeting=(input('''Choose either 'Investment' or 'Bond from the menu below to proceed:

Investment  -  To calculate the amount of interest you will earn on your investment
Bond       -  To calculate the amount you will have to pay on a home loan \n''')).lower().title()

if greeting == "Investment":
      
      investment = int (input('''How much money are you depositing?:          '''))
      rate=float(input('''Enter the percentage of your interest rate:  ''')) / 100             
      years=int(input('''How many years do you plan on investing?:    '''))
      y=str(input("Would you prefer 'Simple' or 'Compound' interest?: ")).lower().title()

elif  greeting == "Bond":
      house_value = int (input('''Please enter the value of the house:   '''))
      monthly_rate=float(input('''Enter the percentage of your interest rate ''')) / 100 /12 
      monthly_repay=int(input('''Please enter the number of months you plan to take to repay the bond'''))
      x = (monthly_rate * house_value)/(1 - (1+monthly_rate)**(-monthly_repay))
      print(f"The monthly repayment is {x:.2f}")      


if     y == 'Simple':
       compound = investment* math.pow((1+rate),years)
       print(f"The total amount of interest you will earn on your investment will be {compound:.2f} ")   

elif   y == "Compound":
       simple = investment*(1+rate*years)
       print(f"The total amount of interest you will earn on your investment will be {simple:.2f} ")

【问题讨论】:

    标签: python nameerror


    【解决方案1】:

    如果输入elif greeting == "Bond":,则y 未定义

    【讨论】:

      【解决方案2】:

      我知道这很简单。感谢您的帮助和时间

      【讨论】:

        【解决方案3】:

        您可以使用嵌套的 if 语句,观察缩进,例如:

        如果问候 == 投资: ... 如果 y == 简单: ... elif y == 复合: ... elif 问候 == 债券:

        【讨论】:

          猜你喜欢
          • 2015-05-06
          • 2018-12-28
          • 2017-08-23
          • 1970-01-01
          • 2013-06-18
          • 1970-01-01
          • 1970-01-01
          • 2015-01-05
          • 1970-01-01
          相关资源
          最近更新 更多