Python 3.x murach python循环增强挑战

Python 3.x murach python循环增强挑战,python-3.x,pseudocode,Python 3.x,Pseudocode,书上说增强后应该是这样 Welcome to the future value calculator enter monthly investment: 0 Entry must be greater than 0 . Please try again. enter monthly investment: 100 enter yearly interest rate: 16 Entry must be greater than 0 and less than or equal to 15. P

书上说增强后应该是这样

Welcome to the future value calculator

enter monthly investment: 0
Entry must be greater than 0 . Please try again.
enter monthly investment: 100
enter yearly interest rate: 16
Entry must be greater than 0 and less than or equal to 15. Please try again.
enter yearly interest rate: 12
Enter number of years: 100
Entry must be greater than 0 and less than or equal to 50
please try again.
enter number of years: 10

Year = 1 Future Value = 1280.93
Year = 2 Future VAlue = 2724.32
Year = 3 Future Value = 4350.76
Year = 4 Future Value = 6183.48
Year = 5 Future Value = 8248.64
Year = 6 Future Value = 10575.7
Year = 7 Future Value = 13197.9
Year = 8 Future Value = 16152.66
Year = 9 Future Value = 19482.15
Year = 10 Future Value = 23233.91

Continue (y/n)?
这就是我在我的程序中所做的

#!/usr/bin/env python3

# display a welcome message
print("Welcome to the Future Value Calculator")
print()

choice = "y"
while choice.lower() == "y":

    # get input from the user
    monthly_investment = float(input("Enter monthly investment:\t"))
    yearly_interest_rate = float(input("Enter yearly interest rate:\t"))
    years = int(input("Enter number of years:\t\t"))

    # convert yearly values to monthly values
    monthly_interest_rate = yearly_interest_rate / 12 / 100
    months = years * 12

    # calculate the future value
    future_value = 0
    for i in range(months):
        future_value += monthly_investment
        monthly_interest_amount = future_value * monthly_interest_rate
        future_value += monthly_interest_amount

    # display the result
    print("Future value:\t\t\t" + str(round(future_value, 2)))
    print()

    # see if the user wants to continue
    choice = input("Continue (y/n)? ")
    print()

print("Bye!")
  • 为每月投资条目添加数据验证。使用while循环检查条目的有效性,并保持循环直到条目有效。若要有效,投资金额必须大于零。如果不是,则应显示一条错误消息,如上面显示的第一条错误消息

  • 使用相同的技术添加利率和年份的数据验证。利率必须大于零且小于或等于15。年份必须大于零,小于等于50。对于每个无效条目,显示并显示相应的错误消息。完成后,将有三个while循环和一个for循环嵌套在另一个while循环中

  • 修改用于计算未来价值的for循环中的语句,以便每年显示一行,显示年份号和未来价值,如图所示。在上面为此,您需要使用range()函数返回的整数

  • 我不太确定我的while循环应该放在哪里开始增强

    请检查下面的代码,让我知道它是否有效

    #!/usr/bin/env python3
    
        # display a welcome message
        print("Welcome to the Future Value Calculator")
        print()
    
        choice = "y"
        while choice.lower() == "y":
    
            # get input from the user
            while True:
                monthly_investment = float(input("Enter monthly investment:  "))
                if monthly_investment <= 0 :
                    print("Entry must be greater than 0 . Please try again.")
                    continue
                else:
                    break
    
            while True:
                yearly_interest_rate = float(input("Enter yearly interest rate:  "))
                if yearly_interest_rate <= 0  or yearly_interest_rate >= 15:
                    print("Entry must be greater than 0 and less than or equal to 15. Please try again.")
                    continue
                else:
                    break
    
            while True:
                years = int(input("Enter number of years: "))
                if years <= 0 or years >= 50:
                    print("Entry must be greater than 0 and less than or equal to 50")
                    continue
                else:
                    break
    
             # convert yearly values to monthly values
            monthly_interest_rate = yearly_interest_rate / 12 / 100
            months = years * 12
    
            # calculate the future value
            future_value = 0
            for i in range(1,months+1):
                future_value += monthly_investment
                monthly_interest_amount = future_value * monthly_interest_rate
                future_value += monthly_interest_amount
                if i % 12 ==0:
                # display the result
                    print("Year = {0} Future value:\t{1}".format(i//12,str(round(future_value, 2))))
    
    
    
            # see if the user wants to continue
            choice = input("Continue (y/n)? ")
            print()
    
        print("Bye!")
    
    #/usr/bin/env蟒蛇3
    #显示欢迎信息
    打印(“欢迎使用未来值计算器”)
    打印()
    choice=“y”
    while choice.lower()=“y”:
    #从用户处获取输入
    尽管如此:
    月度投资=浮动(输入(“输入月度投资:”)
    
    如果你是每月投资的话,请你向我解释一下你编写代码的心理过程。我很高兴它起了作用,但我想有一个更好的理解,这样在将来,如果出现这样的问题,我可以有一些经验来处理它自己。非常感谢。正如为什么break命令将其引导到下一个语句/问题,而不是返回到初始语句/问题一样,Truesure首先,在编写此程序时没有考虑性能。我使用了很多while循环,但是使用单个while循环也可以达到相同的目的。我们有一个无限while循环和许多嵌套的无限while循环。让我们来讨论嵌套while循环。记住,当您处于内部while循环的范围内时,在这种情况下,break总是基于封闭缩进终止。在我们的例子中,这是一个while循环。因此,它与电流while循环断开。其他人也一样。现在让我们讨论for循环,我添加的条件是基于月份的。因此,12的倍数将被计算为年数,仅显示每年的奇特产量。