python - 我怎样才能使这个程序更高效、更短、更简单?
问题描述
我制作了一个 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
但是程序的代码太复杂了。那么,有没有更好的方法可以用更少的代码行更高效地完成相同的操作呢?
更新#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))
我得到的输出是:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12.0, 12.1, 12.2, 12.3, 12.4]
推荐阅读
- javascript - 使用 Jquery 或 JavaScript 更改最大宽度
- linux - Constructing working ELF binary from memory dump
- api - How are Google API quotas calculated?
- onnxruntime - 如何在 ONNX Runtime 中实现多线程?
- python - 如何使用 iterrows() 进行迭代并检查子集
- python - 处理可能的导入错误类型
- c++ - 如何在主线程的 WaitForMultipleObjects 调用中间安全地终止工作线程?
- c# - json as string, not sure if what ive done so far is also correct
- c++ - 出现错误 - 以退出代码 3221225477 完成
- javascript - 如何按字母顺序对div的内容进行排序?