Assistance with faulty GPT code requested /u/upinmyfeelings Python Education

Howdy everyone; I’m very new to python (this is quite literally the first code I’m interacting with). It was constructed with the help of chatgpt which obviously means it isn’t structured correctly. The code is meant to take a given input and a desired output and to determine the settings that each of 8 levers set in sequence (with 16 increments each) should be set to achieve a result as close as possible to the desired output. The first increment on a lever from 1 to 2 changes the ratio of speed from 16:16 to 16:18; and every increment thereafter increases the latter half of the ratio by one (ex. 16:19, 16:20, etc.) up to the max of 16:32 at the maximum increment. If anyone has any help they can provide I would be very appreciative. Still learning how to parse the code so treat me like I’m stupid please 🙂

def simulate_levers(rpm_in, rpm_out, num_levers=8, max_increment=16, epsilon=0.001): # Initialize lever settings and calculate the target ratio lever_settings = [1] * num_levers target_ratio = rpm_out / rpm_in

# Function to calculate the total ratio based on lever settings def calculate_total_ratio(settings): total_ratio = 1 for setting in settings: total_ratio *= 16 / (15 + setting) return total_ratio # Initialize the current ratio current_ratio = calculate_total_ratio(lever_settings) # Iterative adjustment to minimize the error while abs(current_ratio - target_ratio) > epsilon: best_error = float('inf') best_lever = -1 # Test incrementing each lever for lever in range(num_levers): if lever_settings[lever] < max_increment: # Create a copy of the settings and increment the selected lever test_settings = lever_settings[:] test_settings[lever] += 1 test_ratio = calculate_total_ratio(test_settings) error = abs(test_ratio - target_ratio) # If this change reduces the error, update the best lever if error < best_error: best_error = error best_lever = lever # If no improvement is possible, exit the loop if best_lever == -1: break # Apply the best lever adjustment lever_settings[best_lever] += 1 current_ratio = calculate_total_ratio(lever_settings) return lever_settings, current_ratio 

Example usage

rpm_in = 1 rpm_out = 2

settings, achieved_ratio = simulate_levers(rpm_in, rpm_out)

print(“Lever settings:”, settings) print(“Achieved ratio:”, achieved_ratio) print(“Achieved output RPM:”, rpm_in * achieved_ratio

submitted by /u/upinmyfeelings
[link] [comments]

​r/learnpython Howdy everyone; I’m very new to python (this is quite literally the first code I’m interacting with). It was constructed with the help of chatgpt which obviously means it isn’t structured correctly. The code is meant to take a given input and a desired output and to determine the settings that each of 8 levers set in sequence (with 16 increments each) should be set to achieve a result as close as possible to the desired output. The first increment on a lever from 1 to 2 changes the ratio of speed from 16:16 to 16:18; and every increment thereafter increases the latter half of the ratio by one (ex. 16:19, 16:20, etc.) up to the max of 16:32 at the maximum increment. If anyone has any help they can provide I would be very appreciative. Still learning how to parse the code so treat me like I’m stupid please 🙂 def simulate_levers(rpm_in, rpm_out, num_levers=8, max_increment=16, epsilon=0.001): # Initialize lever settings and calculate the target ratio lever_settings = [1] * num_levers target_ratio = rpm_out / rpm_in # Function to calculate the total ratio based on lever settings def calculate_total_ratio(settings): total_ratio = 1 for setting in settings: total_ratio *= 16 / (15 + setting) return total_ratio # Initialize the current ratio current_ratio = calculate_total_ratio(lever_settings) # Iterative adjustment to minimize the error while abs(current_ratio – target_ratio) > epsilon: best_error = float(‘inf’) best_lever = -1 # Test incrementing each lever for lever in range(num_levers): if lever_settings[lever] < max_increment: # Create a copy of the settings and increment the selected lever test_settings = lever_settings[:] test_settings[lever] += 1 test_ratio = calculate_total_ratio(test_settings) error = abs(test_ratio – target_ratio) # If this change reduces the error, update the best lever if error < best_error: best_error = error best_lever = lever # If no improvement is possible, exit the loop if best_lever == -1: break # Apply the best lever adjustment lever_settings[best_lever] += 1 current_ratio = calculate_total_ratio(lever_settings) return lever_settings, current_ratio Example usage rpm_in = 1 rpm_out = 2 settings, achieved_ratio = simulate_levers(rpm_in, rpm_out) print(“Lever settings:”, settings) print(“Achieved ratio:”, achieved_ratio) print(“Achieved output RPM:”, rpm_in * achieved_ratio submitted by /u/upinmyfeelings [link] [comments] 

Howdy everyone; I’m very new to python (this is quite literally the first code I’m interacting with). It was constructed with the help of chatgpt which obviously means it isn’t structured correctly. The code is meant to take a given input and a desired output and to determine the settings that each of 8 levers set in sequence (with 16 increments each) should be set to achieve a result as close as possible to the desired output. The first increment on a lever from 1 to 2 changes the ratio of speed from 16:16 to 16:18; and every increment thereafter increases the latter half of the ratio by one (ex. 16:19, 16:20, etc.) up to the max of 16:32 at the maximum increment. If anyone has any help they can provide I would be very appreciative. Still learning how to parse the code so treat me like I’m stupid please 🙂

def simulate_levers(rpm_in, rpm_out, num_levers=8, max_increment=16, epsilon=0.001): # Initialize lever settings and calculate the target ratio lever_settings = [1] * num_levers target_ratio = rpm_out / rpm_in

# Function to calculate the total ratio based on lever settings def calculate_total_ratio(settings): total_ratio = 1 for setting in settings: total_ratio *= 16 / (15 + setting) return total_ratio # Initialize the current ratio current_ratio = calculate_total_ratio(lever_settings) # Iterative adjustment to minimize the error while abs(current_ratio - target_ratio) > epsilon: best_error = float('inf') best_lever = -1 # Test incrementing each lever for lever in range(num_levers): if lever_settings[lever] < max_increment: # Create a copy of the settings and increment the selected lever test_settings = lever_settings[:] test_settings[lever] += 1 test_ratio = calculate_total_ratio(test_settings) error = abs(test_ratio - target_ratio) # If this change reduces the error, update the best lever if error < best_error: best_error = error best_lever = lever # If no improvement is possible, exit the loop if best_lever == -1: break # Apply the best lever adjustment lever_settings[best_lever] += 1 current_ratio = calculate_total_ratio(lever_settings) return lever_settings, current_ratio 

Example usage

rpm_in = 1 rpm_out = 2

settings, achieved_ratio = simulate_levers(rpm_in, rpm_out)

print(“Lever settings:”, settings) print(“Achieved ratio:”, achieved_ratio) print(“Achieved output RPM:”, rpm_in * achieved_ratio

submitted by /u/upinmyfeelings
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *