example loop that i learned and must follow this loop
maxNo=eval(input("Total number to calculate:"))
maxinner=eval(input("Total number to inner loop:"))
for counterOuter in range(maxNo): #counter condition
#loop body
noOuter=eval(input("enter no:"))
totalOuter=totalOuter+noOuter
totalInner=0
for counterInner in range(3):
noInner=eval(input("enter no inner:"))
totalInner=totalInner+noInner
print("totalInner:", totalInner)
for counterOuter in range(maxNo):
oil_type = input("Oil type: ")
totalInner = 0
for counterInner in range(maxinner): # Pakai variable ni, bukan 3
mass = eval(input("Mass: "))
vol = eval(input("Volume: "))
density = mass / vol
totalInner += density
print(f"Total for {oil_type}: {totalInner}")
tukar jadi range(maxinner) supaya dia ikut input yang user masukkan tadi.
It looks like you want to wrap your oil density calculation logic inside a nested loop structure similar to the one you provided.
To do this effectively, we need to organize the code so the outer loop represents different sessions (or batches of oils) and the inner loop handles the specific oil entries within each session.
Here is the refactored code following your requested structure:
import math
# Initialize variables for the outer loop
total_density_outer = 0
maxNo = eval(input("Total sessions to run (Outer Loop): "))
maxInner = eval(input("Total oils per session (Inner Loop): "))
for counterOuter in range(maxNo):
print(f"\n--- Starting Session {counterOuter + 1} ---")
total_density_inner = 0 # Reset inner total for each new session
for counterInner in range(maxInner):
print(f"\nEntry {counterInner + 1}:")
# 1. Inputs
oil_type = input("Enter oil type (e.g., Palm Oil): ")
mass_oil = eval(input("Enter mass of oil (g): "))
volume_oil = eval(input("Enter volume of oil (mL): "))
# 2. Calculations
density_oil = mass_oil / volume_oil
sqrt_density_oil = math.sqrt(density_oil)
# 3. Multi-selection logic
if density_oil < 0.90:
classification = "Light Oil"
elif density_oil <= 0.93:
classification = "Medium Oil"
else:
classification = "Hard Oil"
# 4. Standard formatted output for this specific entry
print(f"Results for {oil_type}:")
print("Density:", round(density_oil, 2))
print("Sqrt Density:", round(sqrt_density_oil, 2))
print("Classification:", classification)
# Update inner total (using density as an example)
total_density_inner += density_oil
# After inner loop finishes, update and print outer results
print(f"\nTotal Density for Session {counterOuter + 1}: {round(total_density_inner, 2)}")
total_density_outer += total_density_inner
print("\n" + "="*20)
print("Grand Total Density (All Sessions):", round(total_density_outer, 2))
print("thank youuuuuuuuuu")