我怎样才能使这个程序更高效、更简短、更简单?



我编写了一个Python程序,它可以从用户那里获取任何数字(无论是浮点数),Python打印出从1到该数字之间的所有数字。例如,如果我输入12.5,那么Python将显示1、2、3、4、5、6、7、8、9、10、11、12、12.1、12.2、12.3、12.4相同的代码如下:

import math
from decimal import *

user_input = float(input("Enter a number(upto one decimal place): "))
frac, whole = math.modf(user_input)           # frac doesn't give the exact decimal value. For ex. 0.3 = 0.299999999999999999998
frac_actual = Decimal(str(user_input)) % 1 
# Decimal also doesn't give the exact value if we give the parameter as a float, but we can give string float as a parameter(because it gives exact value), with which we can perform the desired operations
if user_input > 0:
if user_input % 1 == 0:
print(f"All the numbers between 0 and {int(user_input)} are: ", end='')
print(*range(1, int(whole) + 1), sep=',', end='')
frac_str = str(frac_actual)          # will convert Decimal('0.something') to ('0.something')
decimal = int(frac_str[2])
else:
print(f"All the numbers between 0 and {user_input} are: ", end='')
print(*range(1, int(whole) + 1), sep=',', end='')
frac_str = str(frac_actual)          # will convert Decimal('0.something') to ('0.something')
decimal = int(frac_str[2])
generator_object = (whole + i / 10 for i in range(1, decimal))
unpacked_generator_object = [*generator_object]
string_generator_object = ','.join(map(str, unpacked_generator_object))
print("," + string_generator_object)

elif user_input == 0:
print("Sorry, numbers between 0 and 0 can't be displayed.")

else:
while user_input < 0:
print("Please enter a positive number or 0.")
user_input = float(input("Enter a number(upto one decimal place): "))
# frac doesn't give the exact decimal value. For ex. 0.3 = 0.299999999999999999998
frac, whole = math.modf(user_input)
frac_actual = Decimal(str(user_input)) % 1
# Decimal also doesn't give the exact value if we give the parameter as a float, but we can give string float as a parameter(because it gives exact value), with which we can perform the desired operations
if user_input % 1 == 0:
print(f"All the numbers between 0 and {int(user_input)} are: ", end='')
print(*range(1, int(whole) + 1), sep=',', end='')
# will convert Decimal('0.something') to ('0.something')
frac_str = str(frac_actual)
decimal = int(frac_str[2])
else:
print(f"All the numbers between 0 and {user_input} are: ", end='')
print(*range(1, int(whole) + 1), sep=',', end='')
# will convert Decimal('0.something') to ('0.something')
frac_str = str(frac_actual)
decimal = int(frac_str[2])
generator_object = (whole + i / 10 for i in range(1, decimal))
unpacked_generator_object = [*generator_object]
string_generator_object = ','.join(map(str, unpacked_generator_object))
print("," + string_generator_object)

输出如下:

Enter a number(upto one decimal place): 15.7
All the numbers between 0 and 15.7 are: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15.1,15.2,15.3,15.4,15.5,15.6

第二个输出:

Enter a number(upto one decimal place): 15
All the numbers between 0 and 15 are: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

但是程序代码太复杂了。那么,有没有更好的方法可以用更少的代码行和更高效地完成相同的操作呢?

UPDATE # 1@Abhishek给出了一个近乎完美的答案。稍作修改后,它看起来像这样:

from decimal import *
def between_nums(n):
frac = float(str(Decimal(str(n)) % 1))
l = []
for x in range(1, int(n) + 1):
l.append(x)
for i in [l[-1] + x / 10 for x in range(1, int(frac * 10))]:
l.append(i)
str_list = ", ".join(map(str, l))
if user_input % 1 == 0:
print(f"All the numbers between 0 and {int(user_input)} are: ", str_list)
else:
print(f"All the numbers between 0 and {user_input} are: ", str_list)
return l
user_input = float(input("Enter a number: "))
between_nums(user_input)

你可以试试这个。如果你不介意列表中的数据,你可以

def between_nums(n):
exp = n % 1
l = []
for x in range(int(n)+1):
l.append(x)
for i in [l[-1]+x / 10 for x in range(0,int(exp*10))]:
l.append(i)
return l

print(between_nums(12.5))

输出i得到:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12.0, 12.1, 12.2, 12.3, 12.4]

最新更新