Working on a weather generator that using terrain and month to randomize a weather effect for that scenario. Its extremely bloated because I could not get while loops or classes to work in previous iterations. I decided to just to make a massive block of code that did the job and revisit later in my project.
Using Python 3.10 is PyCharm IDE. Getting the following error for specific month variables:
in Plains/Hills September and Forest/Hills/Plains November I get an error. Everything else works fine.
Traceback (most recent call last):
File "C:\\Users\\roman\\PycharmProjects\\videogame\\test.py", line 167, in \<module\>
print(weather.current_weather)
AttributeError: type object 'weather' has no attribute 'current_weather'
Septemeber
Plains
script
from random import *
import random
class month:
estimated_day = randint(1, 359)
if estimated_day <= 30:
current_month = ("January")
elif estimated_day <= 60:
current_month = ("February")
elif estimated_day <= 90:
current_month = ("March")
elif estimated_day <= 120:
current_month = ("April")
elif estimated_day <= 150:
current_month = ("May")
elif estimated_day <= 180:
current_month = ("June")
elif estimated_day <= 210:
current_month = ("July")
elif estimated_day <= 240:
current_month = ("August")
elif estimated_day <= 270:
current_month = ("Septemeber")
elif estimated_day <= 300:
current_month = ("October")
elif estimated_day <= 330:
current_month = ("Novemeber")
elif estimated_day <= 360:
current_month = ("December")
print(month.current_month)
terrain_list = ("Forest","Plains","Hills","Tundra","Desert")
current_terrain = (random.choice(terrain_list))
print(current_terrain)
class x:
forest_weather_list = ("Clear","Rain","Thunderstorm","Snowing","Blizzard")
jan_forest_weather_list = ("Clear","Snowing","Blizzard")
feb_forest_weather_list = ("Clear","Snowing","Blizzard")
mar_forest_weather_list = ("Clear","Rain","Thunderstorm")
apr_forest_weather_list = ("Clear","Rain","Thunderstorm")
may_forest_weather_list = ("Clear","Rain","Thunderstorm")
jun_forest_weather_list = ("Clear","Rain","Thunderstorm")
jul_forest_weather_list = ("Clear","Rain","Thunderstorm")
aug_forest_weather_list = ("Clear","Rain","Thunderstorm")
sep_forest_weather_list = ("Clear","Snowing")
oct_forest_weather_list = ("Clear","Snowing")
nov_forest_weather_list = ("Clear","Snowing")
dec_forest_weather_list = ("Clear","Snowing","Blizzard")
plains_weather_list = ("Clear","Rain","Thunderstorm","Snowing","Blizzard")
jan_plains_weather_list = ("Windy","Snowing","Blizzard")
feb_plains_weather_list = ("Windy","Snowing","Blizzard")
mar_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
apr_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
may_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
jun_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
jul_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
aug_plains_weather_list = ("Windy","Rain","Thunderstorm","Tornado")
sep_plains_weather_list = ("Windy","Snowing")
oct_plains_weather_list = ("Windy","Snowing")
nov_plains_weather_list = ("Windy","Snowing")
dec_plains_weather_list = ("Windy","Snowing","Blizzard")
hills_weather_list = ("Clear","Rain","Thunderstorm","Snowing","Blizzard")
jan_hills_weather_list = ("Clear","Snowing","Blizzard")
feb_hills_weather_list = ("Clear","Snowing","Blizzard")
mar_hills_weather_list = ("Clear","Rain","Thunderstorm")
apr_hills_weather_list = ("Clear","Rain","Thunderstorm")
may_hills_weather_list = ("Clear","Rain","Thunderstorm")
jun_hills_weather_list = ("Clear","Rain","Thunderstorm")
jul_hills_weather_list = ("Clear","Rain","Thunderstorm")
aug_hills_weather_list = ("Clear","Rain","Thunderstorm")
sep_hills_weather_list = ("Clear","Snowing")
oct_hills_weather_list = ("Clear","Snowing")
nov_hills_weather_list = ("Clear","Snowing")
dec_hills_weather_list = ("Clear","Snowing","Blizzard")
tundra_weather_list = ("Clear","Snowing","Blizzard")
desert_weather_list = ("Arid","Drought","Sandstorm")
class weather:
if current_terrain == "Forest":
if month.current_month == "January":
current_weather = (random.choice(x.jan_forest_weather_list))
elif month.current_month == "February":
current_weather = (random.choice(x.feb_forest_weather_list))
elif month.current_month == "March":
current_weather = (random.choice(x.mar_forest_weather_list))
elif month.current_month == "April":
current_weather = (random.choice(x.apr_forest_weather_list))
elif month.current_month == "May":
current_weather = (random.choice(x.may_forest_weather_list))
elif month.current_month == "June":
current_weather = (random.choice(x.jun_forest_weather_list))
elif month.current_month == "July":
current_weather = (random.choice(x.jul_forest_weather_list))
elif month.current_month == "August":
current_weather = (random.choice(x.aug_forest_weather_list))
elif month.current_month == "September":
current_weather = (random.choice(x.sep_forest_weather_list))
elif month.current_month == "October":
current_weather = (random.choice(x.oct_forest_weather_list))
elif month.current_month == "November":
current_weather = (random.choice(x.nov_forest_weather_list))
elif month.current_month == "December":
current_weather = (random.choice(x.dec_forest_weather_list))
elif current_terrain == "Plains":
if month.current_month == "January":
current_weather = (random.choice(x.jan_plains_weather_list))
elif month.current_month == "February":
current_weather = (random.choice(x.feb_plains_weather_list))
elif month.current_month == "March":
current_weather = (random.choice(x.mar_plains_weather_list))
elif month.current_month == "April":
current_weather = (random.choice(x.apr_plains_weather_list))
elif month.current_month == "May":
current_weather = (random.choice(x.may_plains_weather_list))
elif month.current_month == "June":
current_weather = (random.choice(x.jun_plains_weather_list))
elif month.current_month == "July":
current_weather = (random.choice(x.jul_plains_weather_list))
elif month.current_month == "August":
current_weather = (random.choice(x.aug_plains_weather_list))
elif month.current_month == "September":
current_weather = (random.choice(x.sep_plains_weather_list))
elif month.current_month == "October":
current_weather = (random.choice(x.oct_plains_weather_list))
elif month.current_month == "November":
current_weather = (random.choice(x.nov_plains_weather_list))
elif month.current_month == "December":
current_weather = (random.choice(x.dec_plains_weather_list))
elif current_terrain == "Hills":
if month.current_month == "January":
current_weather = (random.choice(x.jan_hills_weather_list))
elif month.current_month == "February":
current_weather = (random.choice(x.feb_hills_weather_list))
elif month.current_month == "March":
current_weather = (random.choice(x.mar_hills_weather_list))
elif month.current_month == "April":
current_weather = (random.choice(x.apr_hills_weather_list))
elif month.current_month == "May":
current_weather = (random.choice(x.may_hills_weather_list))
elif month.current_month == "June":
current_weather = (random.choice(x.jun_hills_weather_list))
elif month.current_month == "July":
current_weather = (random.choice(x.jul_hills_weather_list))
elif month.current_month == "August":
current_weather = (random.choice(x.aug_hills_weather_list))
elif month.current_month == "September":
current_weather = (random.choice(x.sep_hills_weather_list))
elif month.current_month == "October":
current_weather = (random.choice(x.oct_hills_weather_list))
elif month.current_month == "November":
current_weather = (random.choice(x.nov_hills_weather_list))
elif month.current_month == "December":
current_weather = (random.choice(x.dec_hills_weather_list))
elif current_terrain == "Tundra":
current_weather = (random.choice(x.tundra_weather_list))
elif current_terrain == "Desert":
current_weather = (random.choice(x.desert_weather_list))
print(weather.current_weather)
If managing the bloat helps I would appreciate that but I really just want to know why 10/12 month variables output my desired result but month nov/sep give an error.
CodePudding user response:
You are approaching the code structure in a very "abstract" way.
What makes a proper variable within a class is using the self "prefix" (trying to simplify terminology)
from random import *
import random
class month:
self.estimated_day = random.randint(1, 359)
self.current_month = ""
def __init__(self):
if self.estimated_day <= 30:
self.current_month = ("January")
elif self.estimated_day <= 60:
self.current_month = ("February")
elif self.estimated_day <= 90:
self.current_month = ("March")
elif self.estimated_day <= 120:
self.current_month = ("April")
elif self.estimated_day <= 150:
self.current_month = ("May")
elif self.estimated_day <= 180:
self.current_month = ("June")
elif self.estimated_day <= 210:
self.current_month = ("July")
elif self.estimated_day <= 240:
self.current_month = ("August")
elif self.estimated_day <= 270:
self.current_month = ("Septemeber")
elif self.estimated_day <= 300:
self.current_month = ("October")
elif self.estimated_day <= 330:
self.current_month = ("Novemeber")
elif self.estimated_day <= 360:
self.current_month = ("December")
Side note, the code is an absolute mess. There are more bugs, so after pasting in this code, it mostly likely still wont work. You need to write the code following the syntax i provided. Use self when needed. Dont inject code raw into a class, use:
def init(): #instead
If you still don't manage to solve it, then I'll take time to correct the code if that's what you need. I would try to explain to help as well if you require it.
CodePudding user response:
You can simplify your logic a lot...
from random import randint
from calendar import month_name
class Month:
def __init__(self):
self.estimated_day = randint(1, 359)
months = month_name[1:]
months = {n:(i 1)*30 for i, n in enumerate(months)}
self.current_month = next(n for n, v in months.items() if self.estimated_day <= v)
test = Month()
print(test.estimated_day, test.current_month)
# Output, we can see that for randomly selected 202, we correctly get July:
202 July