Skip to main content
 首页 » 编程设计

python之如何检查 float

2024年10月01日16emanlee

我正在尝试创建一个嵌套循环来检查用户的输入应该是整数还是 float 。 在我的计算器中,每个化合物的速率存储在基于文本输入的整数中,但是我想为 else 语句添加功能以检查输入是整数还是小数。如果它是一个整数,我希望文本生成不带 .0 的数字,但如果它是一个 float ,例如 6.5,我想将该变量保留为一个 float 。

rerun = True 
 
while (rerun): 
    print ("Welcome to the interest calculator") 
    balance = input ("\nPlease enter your account balance: ") 
    interest = input ("\nWhat is the interest rate on the account? (decimal): ") 
    rate = input ("\nWhat is the rate that interest is applied? " 
                  "\n(Monthly, Quarterly, Annually, Biannually): ") 
    balance = float(balance) 
    interest = float(interest) 
#Convert text to rate variable 
 
    if (rate == "Monthly" or rate == "monthly"): 
        compounds = 12 
    elif (rate == "Quarterly" or rate == "quarterly"): 
        compounds = 4 
    elif (rate == "Annually" or rate == "annually"): 
        compounds = 1 
    elif (rate == "Biannually" or rate == "biannually"): 
        compounds = 2 

这是我认为应该检查的地方

    else: 
        compounds = float(rate) 
#Display Data 
    print ('interest = ',  type(interest)) 
    print ('balance = ',  type(balance)) 
    print ('compounds = ', type(compounds)) 
 
    if (compounds == 1): 
        print ("\nYour account has a balance of $" + str(balance), "dollars with an interest rate \n" 
               "of ", str(interest) + "%", " being applied", str(compounds), "time per year") 
    else: 
        print ("\nYour account has a balance of $" + str(balance), "dollars with an interest rate \n" 
               "of ", str(interest) + "%", " being applied", str(compounds), "times per year") 
 
    total = interest * balance * compounds 
 
    if (total < 1): 
        print ("Calculated total = $" + "{:.2f}".format(total) + " cents") 
    else: 
        print ("Calculated total = $" + "{:.2f}".format(total) + " dollars") 
    #while loop to rerun program 
 
    answer = input ("\nType (y) to rerun the program: ") 
    if (answer == "y" or "Y"): 
        rerun = True 
        print ("\n") 
    else: 
        rerun = False 

因此,如果用户输入 1 作为费率,这将落入 else 语句中,因为它不是我预定义的词之一,“您的帐户余额......”应该将化合物显示为 int。 如果用户输入 1.5 作为利率,这将落在 else 语句中,因为它不是我预定义的词之一,“您的账户余额......”应该将化合物显示为 float 。

任何人都可以就如何处理这个问题向我提供一些意见吗?我试过用余数、减法和加法来写出它来检查化合物是否大于整数,但我似乎无法正确地写出它。

请您参考如下方法:

最初使用float,然后检查is_integer是否为整数并进行相应处理:

>>> f = float(input()) 
3 
>>> f 
3.0 
>>> f.is_integer() 
True 

或者更好的是,使用 g format specifier :

>>> f = 1.000 
>>> f 
1.0 
>>> print("{:g}".format(f)) 
1 
>>> f = 3.14 
>>> print("{:g}".format(f)) 
3.14 

General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.