python-3.x 为什么我的函数会陷入无限循环?

qv7cva1a  于 2023-03-09  发布在  Python
关注(0)|答案(2)|浏览(142)

我最近开始学习Python,感谢您对此的回应。
我故意把**get_int()get_float()的定义写得不同,因为我想了解如果我接受main()**中的输入并在函数调用时传递该值会发生什么。

#!/usr/bin/env python3

def get_int(prompt, low, high):
    while True:
        #number = float(input(prompt))
        if prompt <= low or prompt > high:
            print("Entry should be greater than ", low, "and less than or equal to ", high)
        else:
            return prompt

def get_float(prompt, low, high):
    while True:
        number = float(input(prompt))
        if number <= low or number > high:
            print("Entry should be greater than ", low, "and less than or equal to ", high)
        else:
            return number    
        
def calculate_future_value(monthly_investment, yearly_interest, years):
    # convert yearly values to monthly values
    monthly_interest_rate = yearly_interest / 12 / 100
    months = years * 12

    # calculate future value
    future_value = 0.0
    for i in range(months):
        future_value += monthly_investment
        monthly_interest = future_value * monthly_interest_rate
        future_value += monthly_interest

    return future_value

def main():
    years = 0
    monthly_investment = 0.0
    yearly_interest_rate = 0.0
    choice = "y"
    while choice.lower() == "y":
        # get input from the user
        monthly_investment = get_float("Enter monthly investment: ", 0, 1000)
        yearly_interest_rate = get_float("Enter yearly interest rate: ", 0, 15)
        years = int(input("Enter number of years: "))
        years = get_int(years, 0, 15)

        # get and display future value
        future_value = calculate_future_value(
            monthly_investment, yearly_interest_rate, years)

        print(f"Future value:\t\t\t{round(future_value, 2)}")
        print()

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

    print("Bye!")
    
if __name__ == "__main__":
    main()

我只是在get_int()上有问题。正如你所看到的,我传递了low=0high=15。如果我输入的年份为0或16,那么它会显示“if”子句中指定的无限打印语句,但当我输入范围内的值时没有问题(见屏幕截图:https://drive.google.com/drive/u/0/folders/1w4-IhBveZQv0pSDygc1HexNFBDUDNE-B)。
我不完全确定是否可以直接在函数定义中使用函数参数而不给局部变量赋值。
如果确实允许,那么**get_int()**中的“while”循环是否有错误?或者我是否在main()本身中包含了while循环,在那里我接受int值?
先谢了!

jtoj6r0c

jtoj6r0c1#

get_int中的while True:实际上是一个无限循环,while可以通过执行return(在else情况下执行)或break语句来打破。由于在if中不执行任何类似的语句,因此循环继续。
正如您的问题所示,您根本不需要循环,因此,您可以将get_int声明为:

def get_int(prompt, low, high):
    #number = float(input(prompt))
    if prompt <= low or prompt > high:
        print("Entry should be greater than ", low, "and less than or equal to ", high)
    else:
        return prompt
2w3rbyxf

2w3rbyxf2#

如果prompt不正确,则不更新其值;如果不更新其值,则条件不变;如果不变,则只打印文本,不再返回。

如果prompt值错误,请刷新它

如果用户输入错误,你仍然需要刷新某个地方的值,如果你不想在循环开始时询问,那么在提示错误时询问。

def get_int(prompt, low, high):
    while True:
        if prompt <= low or prompt > high:
            print("Entry should be greater than ", low, "and less than or equal to ", high)
            prompt = int(input("Enter a new value"))  # You need to ask a new value
        else:
            return prompt

替代品:

您可以将if条件直接放在while循环中:

def get_int(prompt, low, high):
    while prompt <= low or prompt > high:
        print("Entry should be greater than ", low, "and less than or equal to ", high)
        prompt = int(input("Enter a new value"))
    return prompt

这样做,如果提示正确,就立即退出循环。如果不正确,就打印消息并刷新值。

相关问题