W oparciu o treść wpisu na forum: Proste procenty i liczby na ich podstawie [ Matematyka, fizyka, logika ] Postanowiłem w ramach ćwiczenia z uwagi na to, że dopiero od niedawna próbuje swoich sił w nauce języka programowania python, napisać skrypt do rozwiązania problemu tam postawionego. Proszę o ewentualne uwagi, co można było zapisać lepiej, inaczej (lepsza wydajność wykonywanego kodu), co dodać itp. Z góry dziękuję.
Wersja oryginalna - pierwsza publikacja posta.
from termcolor import colored # https://pypi.org/project/termcolor/
import random
def left(string,length): return string[:length] # funkcja wykorzystywana
def right(string,length): return string[-length:] # funkcja nieużywana - pozostawiona - cel edukacyjny
def mid(string,start,length): return string[start:start+length] # funkcja wykorzystywana
how_many_tries = 10
how_many_tries_count = 1
how_many_tries_count_str = ""
range_min = 0
range_max = 0
range_min_str = ""
range_max_str = ""
number_from_range = 0
number_from_range_str = ""
calculate_percent_str = ""
calculate_percent_str_left = ""
calculate_percent_str_right = ""
string_line_buff = ""
print()
while how_many_tries > 0:
range_min = random.randint(1,99)
range_max = random.randint(100,200)
number_from_range = random.randint(range_min + 1,range_max -1)
range_min_str = str(range_min)
range_max_str = str(range_max)
number_from_range_str = str(number_from_range)
how_many_tries_count_str = str(how_many_tries_count)
string_line_buff = "Próba " + colored(how_many_tries_count_str.rjust(2," "),"green",attrs=["bold"]) + ". "
string_line_buff += "Zakres roboczy: "
string_line_buff += colored(range_min_str.rjust(2," "),"yellow",attrs=["bold"]) + " [" + colored("0%",attrs=["bold"]) + "] - "
string_line_buff += colored(range_max_str,"yellow",attrs=["bold"]) + " [" + colored("100%",attrs=["bold"]) + "]. "
string_line_buff += "Liczba " + colored(number_from_range_str.ljust(3," "),"green",attrs=["bold"]) + " z ustalonego zakresu stanowi "
calculate_percent_str = str(round((((number_from_range - range_min) / (range_max - range_min)) * 100),2))
calculate_percent_str_left = left(calculate_percent_str,calculate_percent_str.find("."))
calculate_percent_str_left = calculate_percent_str_left.rjust(2," ")
calculate_percent_str_right = mid(calculate_percent_str,calculate_percent_str.find(".") + 1,2)
calculate_percent_str_right = calculate_percent_str_right.strip()
calculate_percent_str_right = calculate_percent_str_right.ljust(2,"0")
string_line_buff += colored(calculate_percent_str_left + "." + calculate_percent_str_right,"magenta") + "%"
print(string_line_buff)
how_many_tries_count += 1
how_many_tries -= 1
Wersja z poprawkami zasugerowanymi przez: adrian17 (Dzięki wielkie)
from termcolor import colored # https://pypi.org/project/termcolor/
import random
how_many_tries = 10
# range_min = 0
# range_max = 0
# number_from_range = 0
# line_buffor = ""
output_line = ""
print()
for loop in range(how_many_tries):
range_min = random.randint(1,99)
range_max = random.randint(100,200)
number_from_range = random.randint(range_min + 1,range_max -1)
percent = ((number_from_range - range_min) / (range_max - range_min)) * 100
line_buffor = "Próba {}. Zakres roboczy: {} [{}] - {} [{}]. Liczba {} z ustalonego zakresu stanowi {}%\n"
line_buffor = line_buffor.format(
colored("{:>2}".format(loop+1), "green", attrs=["bold"]),
colored("{:>2}".format(range_min), "yellow", attrs=["bold"]),
colored("0%", attrs=["bold"]),
colored("{:>2}".format(range_max), "yellow", attrs=["bold"]),
colored("100%", attrs=["bold"]),
colored("{:<3}".format(number_from_range), "green", attrs=["bold"]),
colored("{:>5.2f}".format(percent), "magenta")
)
output_line += line_buffor
print(output_line)