Oct-02-2024, 08:34 AM
I'm using Pycharm as a Python IDE. Is it advisable to do so?
I wrote a program and it gives me the following error messages (the code works, Pycharm says its a warning and a weak warning)
I wrote a program and it gives me the following error messages (the code works, Pycharm says its a warning and a weak warning)
Error:Name 'length' can be undefined
Name 'n' can be undefined
Multi-step list initialization can be replaced with a list literal
My code is (if required to help me):numerator = float(input("Enter numerator: ")) denominator = float(input("Enter denominator: ")) numerator = int(round((numerator * 100 / denominator), 0)) denominator = 100 numbers_list = [] numbers_list.append(numerator) numbers_list.append(numerator - 1) sum_is_1 = False difference_is_0 = False test_number_1 = (numerator / 100) + ((numerator - 1) / 100) doubling = 1 while not sum_is_1 and not difference_is_0: length = len(numbers_list) fractions_total = 0 doubling = doubling * 2 for i in range(length): numbers_list[i] = numbers_list[i] * 2 for i in range(length - 1): numbers_list.insert(i + 1, numbers_list[i] - 1) length = len(numbers_list) for i in range(length): fractions_total = fractions_total + numbers_list[i] / (100 * doubling) test_number_2 = fractions_total if fractions_total >= 1: sum_is_1 = True difference = abs(test_number_1 - test_number_2) test_number_1 = test_number_2 if difference <= 0.0001: difference_is_0 = True print("For fraction " + str(numerator / denominator) + ", we can construct fractions <= " + str(numerator / denominator) + " such that their sum = 1") print() print("The list of numbers that had to be created") print(numbers_list) print() print("the fractions are: ") for i in range(length): print(numbers_list[i] / (100 * doubling)) print() sum_1 = 0 for i in range(length): sum_1 = sum_1 + numbers_list[i] / (100 * doubling) sum_2 = sum_1 + numbers_list[i + 1] / (100 * doubling) if sum_2 >= 1: n = i + 1 # excluded break sum_1 = 0 for i in range(n): sum_1 = sum_1 + (numbers_list[i] / (100 * doubling)) other_fraction = round(1 - sum_1, 5) print("The fractions are: ") for i in range(n): print(numbers_list[i] / (100 * doubling)) print(other_fraction)What I intend to do is start off with a fraction. Say 3/10. We get to the next smaller fraction 2/10. Then I multiply both fractions by 2/2 = 1 and we get 6/20, 4/20. I can then get an in-between fraction 5/20. I repeat the process. I add these fractions up and check if the sum >= 1.