Last active
September 26, 2020 16:15
-
-
Save XCanG/af9ad0b2e25c2b2033917d7a18dcc07d to your computer and use it in GitHub Desktop.
This module generate float range of numbers. Run script either with double click, it will prompt for input, or from console like that or without arguments: python print_numbers.py <from> <to> [step] [acceleration]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
import sys | |
import os | |
def print_numbers(start, end, step = .1, acceleration = .0): | |
if end < start and step > 0.: | |
start, end = end, start | |
if start < end and step < 0.: | |
start, end = end, start | |
if step == 0.: | |
print(start) | |
return | |
precision = max(str(start)[::-1].find("."), str(end)[::-1].find("."), str(step)[::-1].find("."), str(acceleration)[::-1].find(".")) | |
start = round(start, precision) | |
end = round(end, precision) | |
current = start | |
while current <= end: | |
print(f"{current:.0{precision}f}") | |
current = round(current + step, precision) | |
if acceleration: | |
step = round(step + acceleration, precision) | |
if step <= 0: | |
print() | |
break | |
if end < current < round(end + step, precision): | |
print(f"{end:.0{precision}f}") | |
def _exit(): | |
try: | |
if os.name == "nt": | |
os.system("pause") | |
else: | |
input("\nPause before quitting") | |
sys.exit() | |
except SyntaxError: | |
sys.exit() | |
if __name__ == "__main__": | |
args = sys.argv[1:] | |
if len(args) < 2: | |
try: | |
numbers = list(map(float, input("Type float numbers from and to (optionally type step, default 0.1 and acceleration, default 0.0), e.g.:\n1.0 10.0\n2.0 3.0 0.05\n0.0 4.0 0.0 0.1\n\n> ").split()[:4])) | |
except ValueError: | |
print("Numbers are not correct.") | |
_exit() | |
if len(numbers) < 2: | |
print("Need to type at least starting and ending number.") | |
_exit() | |
else: | |
try: | |
numbers = list(map(float, args[:4])) | |
except ValueError: | |
print("Numbers are not correct.") | |
_exit() | |
print("") | |
print_numbers(*numbers) | |
_exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment