Question: I need help with my python code, I ' m a beginner and I don't know what I ' m doing wrong. The assignment instructions

I need help with my python code, I'm a beginner and I don't know what I'm doing wrong. The assignment instructions is in the screenshots. The test.py file runs my code which was written by the teacher. I can't fully add it here since there's not enough characters available. Is it also possible to compress my code without getting rid of the methods used? This is my code (Assignment2.py):
import numpy as np
class WeatherSimulation:
def __init__(self, transition_probabilities, holding_times):
self.transition_probabilities = transition_probabilities
self.holding_times = holding_times
# Validate transition probabilities
for state, transitions in self.transition_probabilities.items():
if not np.isclose(sum(transitions.values()),1.0):
raise RuntimeError(f"Transition probabilities for state '{state}' do not sum to 1.")
self._current_state = 'sunny' # Start with 'sunny' state
self._remaining_hours = holding_times[self._current_state]
def get_states(self):
return list(self.transition_probabilities.keys())
def set_state(self, state):
# Set the weather state manually and reset remaining hours for that state
if state in self.transition_probabilities:
self._current_state = state
self._remaining_hours = self.holding_times[state]
else:
raise ValueError(f"State '{state}' is not a valid weather state.")
def current_state(self):
# Return the current weather state
return self._current_state
def current_state_remaining_hours(self):
# Return the remaining hours for the current state
return self._remaining_hours
def next_state(self):
# Transition to the next weather state based on probabilities and reset remaining hours
next_state = np.random.choice(
list(self.transition_probabilities[self._current_state].keys()),
p=list(self.transition_probabilities[self._current_state].values())
)
print(f"Transitioning from {self._current_state} to {next_state}")
self._current_state = next_state
self._remaining_hours = self.holding_times[next_state]
def set_holding_time_in_state(self, state, hours):
# Set holding time for a specific weather state
if state in self.holding_times:
self.holding_times[state]= hours
else:
raise ValueError(f"State '{state}' does not exist.")
def iterable(self):
# This method returns an iterable generator that simulates the weather state transitions
while True:
yield self._current_state, self._remaining_hours
self._remaining_hours -=1
print(f"Iterating: State: {self._current_state}, Remaining Hours: {self._remaining_hours}")
if self._remaining_hours =0:
self.next_state()
def simulate(self, hours):
occurrences ={state: 0 for state in self.get_states()}
total_hours =0
while total_hours hours:
occurrences[self._current_state]+=1
self._remaining_hours -=1
total_hours +=1
if self._remaining_hours =0:
self.next_state()
self._remaining_hours = self.holding_times[self._current_state] # Reset after transition
total = sum(occurrences.values())
return [occurrences[state]/ total *100 for state in self.get_states()]
# Example data for transition probabilities and holding times
my_transitions ={
'sunny': {'sunny': 0.7, 'cloudy': 0.3, 'rainy': 0, 'snowy': 0},
'cloudy': {'sunny': 0.5, 'cloudy': 0.3, 'rainy': 0.15, 'snowy': 0.05},
'rainy': {'sunny': 0.6, 'cloudy': 0.2, 'rainy': 0.15, 'snowy': 0.05},
'snowy': {'sunny': 0.7, 'cloudy': 0.1, 'rainy': 0.05, 'snowy': 0.15}
}
my_holding_times ={'sunny': 1, 'cloudy': 2, 'rainy': 2, 'snowy': 1}
# Create the simulation object
simulation = WeatherSimulation(my_transitions, my_holding_times)
# Run the simulation for 10_000 hours
result = simulation.simulate(10000)
print("Percentage of time each state occurred: ", result)
END of my code. When running it through the test file it gets stuck on NOK! which means not ok:
if not check_holding_times(transitions, holding_time):
print("Probably a problem with holding times")
sys.exit('NOK!')
else:
print("Correct holding times")
I need help with my python code, I ' m a beginner

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!