使用地形和月份來隨機化該場景的天氣效果的天氣生成器。它非常臃腫,因為我無法讓 while 回圈或類在以前的迭代中作業。我決定只撰寫大量代碼塊來完成這項作業,然后在我的專案中重新訪問。
使用 Python 3.10 的是 PyCharm IDE。獲取特定月份變數的以下錯誤:
在 Plains/Hills September 和 Forest/Hills/Plains November 我得到一個錯誤。其他一切正常。
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
腳本
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)
如果管理膨脹有幫助,我將不勝感激,但我真的只想知道為什么 10/12 月變數輸出我想要的結果,但月 nov/sep 給出錯誤。
uj5u.com熱心網友回復:
您正在以非常“抽象”的方式處理代碼結構。
使類中的適當變數的原因是使用自我“前綴”(試圖簡化術語)
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")
旁注,代碼絕對是一團糟。有更多的錯誤,所以在粘貼這段代碼之后,它很可能仍然無法作業。您需要按照我提供的語法撰寫代碼。需要時使用self。不要將原始代碼注入到類中,請使用:
def init (): #改為
如果您仍然無法解決它,那么如果您需要,我會花時間更正代碼。如果您需要,我也會嘗試解釋以提供幫助。
uj5u.com熱心網友回復:
你可以大大簡化你的邏輯......
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/525743.html
