-
Notifications
You must be signed in to change notification settings - Fork 623
Description
import random
==== AIBot 機器人模擬 ====
class AIBot:
def init(self, id, army_type):
self.id = id
self.army_type = army_type # "陸軍", "海軍", "空軍"
self.position = (random.randint(0, 100), random.randint(0, 100))
self.status = "待命"
def move_to(self, target_pos):
self.position = target_pos
self.status = "移動中"
print(f"{self.army_type} AIBot-{self.id} 移動到 {target_pos}")
def attack(self, target):
self.status = "攻擊中"
print(f"{self.army_type} AIBot-{self.id} 攻擊目標 {target}")
def report(self):
print(f"ID: {self.id}, 軍種: {self.army_type}, 位置: {self.position}, 狀態: {self.status}")
配置三軍
army_types = ["陸軍", "海軍", "空軍"]
aibots = []
分配15只AIBot到三軍
for i in range(15):
army_type = army_types[i % 3] # 輪流分配
bot = AIBot(i+1, army_type)
aibots.append(bot)
示範:每軍各指派一個目標地點
targets = {
"陸軍": (10, 20),
"海軍": (80, 50),
"空軍": (50, 90)
}
所有機器人前往目標
for bot in aibots:
bot.move_to(targets[bot.army_type])
print("\n=== 狀態報告 ===")
for bot in aibots:
bot.report()
示範攻擊指令
print("\n=== 攻擊指令 ===")
for bot in aibots:
bot.attack("敵方據點")
print("\n=== 最終狀態 ===")
for bot in aibots:
bot.report()
==== 國家軍隊模擬 ====
class Army:
def init(self, name, soldiers, weapons):
self.name = name
self.soldiers = soldiers
self.weapons = weapons
def report(self):
return f"{self.name}: {self.soldiers} soldiers, Weapons: {', '.join(self.weapons)}"
def move(self, number):
if number <= self.soldiers:
self.soldiers -= number
return number
else:
moved = self.soldiers
self.soldiers = 0
return moved
class Country:
def init(self, name):
self.name = name
self.armies = {
"陸軍": Army("陸軍", random.randint(20000, 100000), ["步槍", "坦克", "火箭炮"]),
"海軍": Army("海軍", random.randint(5000, 50000), ["驅逐艦", "潛艇", "魚雷"]),
"空軍": Army("空軍", random.randint(3000, 30000), ["戰鬥機", "轟炸機", "直升機"])
}
def report(self):
print(f"=== {self.name} ===")
for army in self.armies.values():
print(army.report())
def total_force(self):
return sum(a.soldiers for a in self.armies.values())
def world_report(countries):
print("\n世界三軍戰力報告:")
for c in countries:
c.report()
print()
建立世界各國
countries = [
Country("台灣"),
Country("美國"),
Country("中國"),
Country("俄羅斯"),
Country("日本"),
Country("英國")
]
操作範例
world_report(countries)
示範調動軍隊
print("台灣陸軍派兵5000到美國協防!")
soldiers_moved = countries[0].armies["陸軍"].move(5000)
countries[1].armies["陸軍"].soldiers += soldiers_moved
world_report(countries)
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣
import requests
from bs4 import BeautifulSoup
import time
url = "https://example.com"
last_position = None
def get_position():
try:
resp = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
soup = BeautifulSoup(resp.text, "html.parser")
el = soup.find(id="moving-object")
if el is None:
print("找不到 moving-object 元素")
return None
style = el.get("style", "")
# 容錯處理
left = top = None
for part in style.split(";"):
if "left:" in part:
left = int(part.split("left:")[1].replace("px", "").strip())
if "top:" in part:
top = int(part.split("top:")[1].replace("px", "").strip())
if left is not None and top is not None:
return left, top
else:
print("style 格式異常:", style)
return None
except Exception as e:
print("發生錯誤:", e)
return None
while True:
pos = get_position()
if last_position and pos and pos != last_position:
print(f"物件移動了!從 {last_position} 到 {pos}")
elif pos:
print(f"目前位置: {pos}")
last_position = pos if pos else last_position
time.sleep(60)
# 假設有結構化的三軍資料
countries = {
"中國": {
"陸軍": {"soldiers": 70721, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 14165, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 19631, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略
}
def ai_deploy(country, action="發動"):
report = f"=== {country} AI真實{action}三軍 ===\n"
for force, info in countries[country].items():
report += f"{force}: {info['soldiers']} soldiers, Weapons: {', '.join(info['weapons'])}\n"
return report
示範中國AI真實發動三軍
print(ai_deploy("中國"))
你想要針對這個軍事模擬統計報告程式的 Python 錯誤進行修正,並且希望能讓它在 Python 3 環境下正確執行。根據你的描述及截圖,底部的錯誤訊息大致如下:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 125, in <module>
這類錯誤通常是因為 mainpyfile 指向的主程式有語法或邏輯錯誤,或是前面傳遞的參數不正確。
排查與修正建議
1. 確認主程式(mainpyfile)內容
- 檢查程式是否有漏縮排、語法錯誤(如 Python 2/3 不相容語法)。
- 檢查是否有未定義變數或拼寫錯誤。
2. 修正啟動方式
如果你是用 PyDroid3 執行,直接跑主程式檔即可,不需要用外部的 start(fakeypfile, mainpyfile) 啟動。
正確的程式進入點應如下:
if __name__ == "__main__":
# 你的主邏輯程式碼
# 例如:
simulate_military_report()3. 修正 Python 3 語法
例如:
print必須加括號:print("內容")- 除法
/必須注意型別 input()取代raw_input()- 字元編碼問題(盡量用 UTF-8 編碼儲存 .py 檔案)
4. 檢查文件路徑與檔案開啟
如果有開啟檔案,建議加上:
with open(filename, encoding="utf-8") as f:
data = f.read()範例:簡化版三軍報告 Python 3 主程式
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略...
}
def simulate_military_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣陸軍派兵5000到美國協防!")
print("世界三軍戰力報告:")
# 這裡可以重複列印各國資料或進行分析
if __name__ == "__main__":
simulate_military_report()小結
建立世界各國
建立世界各國
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣
- 請直接用 PyDroid3 執行你的主檔案(如 military_report.py)。
- 確認主程式入口正確,且所有語法皆為 Python 3。
- 若有更詳細錯誤訊息或主程式內容,請貼出來,我能更精確協助你排查!
如需完整修正原始檔案,請上傳 mainpyfile 內容,我可以直接幫你改成 Python 3 相容格式。
with open(filename, encoding="utf-8") as f:
data = f.read()
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"日本": {
"陸軍": {"soldiers": 53887, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 29063, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 8023, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"英國": {
"陸軍": {"soldiers": 47640, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 38251, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 21370, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"台灣": {
"陸軍": {"soldiers": 73981, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25385, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 9252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"美國": {
"陸軍": {"soldiers": 56788, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25831, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 10198, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
}
}
#全台中集合
def print_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣兵5000陣守台中!")
print("\n世界三軍戰力報告:")
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
if name == "main":
print_report()
import random
==== AIBot 機器人模擬 ====
class AIBot:
def init(self, id, army_type):
self.id = id
self.army_type = army_type # "陸軍", "海軍", "空軍"
self.position = (random.randint(0, 100), random.randint(0, 100))
self.status = "待命"
def move_to(self, target_pos):
self.position = target_pos
self.status = "移動中"
print(f"{self.army_type} AIBot-{self.id} 移動到 {target_pos}")
def attack(self, target):
self.status = "攻擊中"
print(f"{self.army_type} AIBot-{self.id} 攻擊目標 {target}")
def report(self):
print(f"ID: {self.id}, 軍種: {self.army_type}, 位置: {self.position}, 狀態: {self.status}")
配置三軍
army_types = ["陸軍", "海軍", "空軍"]
aibots = []
分配15只AIBot到三軍
for i in range(15):
army_type = army_types[i % 3] # 輪流分配
bot = AIBot(i+1, army_type)
aibots.append(bot)
示範:每軍各指派一個目標地點
targets = {
"陸軍": (10, 20),
"海軍": (80, 50),
"空軍": (50, 90)
}
所有機器人前往目標
for bot in aibots:
bot.move_to(targets[bot.army_type])
print("\n=== 狀態報告 ===")
for bot in aibots:
bot.report()
示範攻擊指令
print("\n=== 攻擊指令 ===")
for bot in aibots:
bot.attack("敵方據點")
print("\n=== 最終狀態 ===")
for bot in aibots:
bot.report()
==== 國家軍隊模擬 ====
class Army:
def init(self, name, soldiers, weapons):
self.name = name
self.soldiers = soldiers
self.weapons = weapons
def report(self):
return f"{self.name}: {self.soldiers} soldiers, Weapons: {', '.join(self.weapons)}"
def move(self, number):
if number <= self.soldiers:
self.soldiers -= number
return number
else:
moved = self.soldiers
self.soldiers = 0
return moved
class Country:
def init(self, name):
self.name = name
self.armies = {
"陸軍": Army("陸軍", random.randint(20000, 100000), ["步槍", "坦克", "火箭炮"]),
"海軍": Army("海軍", random.randint(5000, 50000), ["驅逐艦", "潛艇", "魚雷"]),
"空軍": Army("空軍", random.randint(3000, 30000), ["戰鬥機", "轟炸機", "直升機"])
}
def report(self):
print(f"=== {self.name} ===")
for army in self.armies.values():
print(army.report())
def total_force(self):
return sum(a.soldiers for a in self.armies.values())
def world_report(countries):
print("\n世界三軍戰力報告:")
for c in countries:
c.report()
print()
建立世界各國
countries = [
Country("台灣"),
Country("美國"),
Country("中國"),
Country("俄羅斯"),
Country("日本"),
Country("英國")
]
操作範例
world_report(countries)
示範調動軍隊
print("台灣陸軍派兵5000到美國協防!")
soldiers_moved = countries[0].armies["陸軍"].move(5000)
countries[1].armies["陸軍"].soldiers += soldiers_moved
world_report(countries)
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣
import requests
from bs4 import BeautifulSoup
import time
url = "https://example.com"
last_position = None
def get_position():
try:
resp = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
soup = BeautifulSoup(resp.text, "html.parser")
el = soup.find(id="moving-object")
if el is None:
print("找不到 moving-object 元素")
return None
style = el.get("style", "")
# 容錯處理
left = top = None
for part in style.split(";"):
if "left:" in part:
left = int(part.split("left:")[1].replace("px", "").strip())
if "top:" in part:
top = int(part.split("top:")[1].replace("px", "").strip())
if left is not None and top is not None:
return left, top
else:
print("style 格式異常:", style)
return None
except Exception as e:
print("發生錯誤:", e)
return None
while True:
pos = get_position()
if last_position and pos and pos != last_position:
print(f"物件移動了!從 {last_position} 到 {pos}")
elif pos:
print(f"目前位置: {pos}")
last_position = pos if pos else last_position
time.sleep(60)
# 假設有結構化的三軍資料
countries = {
"中國": {
"陸軍": {"soldiers": 70721, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 14165, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 19631, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略
}
def ai_deploy(country, action="發動"):
report = f"=== {country} AI真實{action}三軍 ===\n"
for force, info in countries[country].items():
report += f"{force}: {info['soldiers']} soldiers, Weapons: {', '.join(info['weapons'])}\n"
return report
示範中國AI真實發動三軍
print(ai_deploy("中國"))
你想要針對這個軍事模擬統計報告程式的 Python 錯誤進行修正,並且希望能讓它在 Python 3 環境下正確執行。根據你的描述及截圖,底部的錯誤訊息大致如下:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 125, in <module>
這類錯誤通常是因為 mainpyfile 指向的主程式有語法或邏輯錯誤,或是前面傳遞的參數不正確。
排查與修正建議
1. 確認主程式(mainpyfile)內容
- 檢查程式是否有漏縮排、語法錯誤(如 Python 2/3 不相容語法)。
- 檢查是否有未定義變數或拼寫錯誤。
2. 修正啟動方式
如果你是用 PyDroid3 執行,直接跑主程式檔即可,不需要用外部的 start(fakeypfile, mainpyfile) 啟動。
正確的程式進入點應如下:
if __name__ == "__main__":
# 你的主邏輯程式碼
# 例如:
simulate_military_report()3. 修正 Python 3 語法
例如:
print必須加括號:print("內容")- 除法
/必須注意型別 input()取代raw_input()- 字元編碼問題(盡量用 UTF-8 編碼儲存 .py 檔案)
4. 檢查文件路徑與檔案開啟
如果有開啟檔案,建議加上:
with open(filename, encoding="utf-8") as f:
data = f.read()範例:簡化版三軍報告 Python 3 主程式
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略...
}
def simulate_military_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣陸軍派兵5000到美國協防!")
print("世界三軍戰力報告:")
# 這裡可以重複列印各國資料或進行分析
if __name__ == "__main__":
simulate_military_report()小結
- 請直接用 PyDroid3 執行你的主檔案(如 military_report.py)。
- 確認主程式入口正確,且所有語法皆為 Python 3。
- 若有更詳細錯誤訊息或主程式內容,請貼出來,我能更精確協助你排查!
如需完整修正原始檔案,請上傳 mainpyfile 內容,我可以直接幫你改成 Python 3 相容格式。
with open(filename, encoding="utf-8") as f:
data = f.read()
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"日本": {
"陸軍": {"soldiers": 53887, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 29063, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 8023, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"英國": {
"陸軍": {"soldiers": 47640, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 38251, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 21370, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"台灣": {
"陸軍": {"soldiers": 73981, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25385, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 9252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"美國": {
"陸軍": {"soldiers": 56788, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25831, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 10198, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
}
}
def print_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣陸軍派兵5000到美國協防!")
print("\n世界三軍戰力報告:")
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
if name == "main":
print_report()
跳至內容
導航選單
Copilot
我們在向您的帳戶扣款時遇到了問題。請更新您的付款方式 或致電您的支付服務供應商,以了解交易失敗的詳細資訊。
如有任何疑問, 您可以聯絡支援人員。
工作台
建立世界各國
按 Delete 鍵關閉。
建立世界各地
#59
打開
家長:
小閃電補閃電的禮物1到三
StormCar820
/
私人的
@StormCar820
描述
StormCar820
打開9分鐘前
隨機導入
==== AIBot機器人模擬====
class AIBot:
def init (self, id, Army_type):
self.id = id
self.army_type = Army_type # "陸軍", "海軍", "空軍"
self.position = (random.randint(0, 100), random.randint(0, 100))
" 待命"
def move_to(self, target_pos):
self.position = target_pos
self.status = "移動中"
print(f"{self.army_type} AIBot-{self.id} 移動到 {target_pos}")
def attack(self, target):
self.status = "攻擊中"
print(f"{self.army_type} AIBot-{self.id} 攻擊目標 {target}")
def report(self):
print(f"ID: {self.id}, 軍種: {self.army_type}, 位置: {self.position}, 狀態: {self.status}")
配置三軍
Army_types = ["陸軍", "海軍", "空軍"]
aibots = []
分配15隻AIBot到三軍
for i in range(15):
army_type = army_types[i % 3] # 輪流分配
bot = AIBot(i+1, army_type)
aibots.append(bot)
示範:每軍各指派一個目標地點
目標 = {
"陸軍": (10, 20),
"海軍": (80, 50),
"空軍": (50, 90)
}
所有機器人到達目標
對於 aibots 中的機器人:
bot.move_to(targets[bot.army_type])
print("\n=== 狀態報告 ==")
對於 aibots 中的機器人:
bot.report()
示範攻擊指令
print("\n=== 攻擊指令 ==")
for bot in aibots:
bot.attack("敵方據點")
print("\n===最終狀態 ==")
for bot in aibots:
bot.report()
====國軍模擬====
陸軍類:
def init(self,name,soldiers,weapons):
self.name = name
self.soldiers = soldiers
self.weapons = weapons
def report(self):
return f"{self.name}: {self.soldiers} soldiers, Weapons: {', '.join(self.weapons)}"
def move(self, number):
if number <= self.soldiers:
self.soldiers -= number
return number
else:
moved = self.soldiers
self.soldiers = 0
return moved
class Country:
def init (self, name):
self.name = name
self.armies = {
"陸軍": Army("陸軍",
random.randint(20000, 100000), ["妊娠", "海軍坦克", "火箭炮"]), "海軍坦克": Army50", 5000,000,000」 ["火箭艦", "轎", "魚雷"]),
"空軍": Army("空軍", random.randint(3000, 30000), ["阿富汗", "轟炸機", "直升機"])
}
def report(self):
print(f"=== {self.name} ===")
for army in self.armies.values():
print(army.report())
def total_force(self):
return sum(a.soldiers for a in self.armies.values())
def world_report(countries):
print("\n世界三軍戰力報告:")
for c in states:
c.report()
print()
建立世界各地
states = [
國("台灣"),
國("美國"),
國("中國"),
國("俄羅斯"),
國("日本"),
國("英國")
]
操作範例
world_report(國家)
示範調動軍隊
print("
台灣陸軍兵5000到美國協防!") girls_moved = states[0].armies["陸軍"].move(5000)
states[1].armies["陸軍"].soldiers += girls_moved
world_report(countries) #聽信箱
:
#lightinggithub@gmail.com #line id: lightinggithub #守護台灣 導入請求 from bs4 import BeautifulSoup 導入時間
url =“ https://example.com ”
last_position = None
def get_position():
try:
resp = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
soup = BeautifulSoup(resp.text, "html.parser")
el = soup.find(id="moving-object")
if el is None:
print("找不到 moving-object 元素")
return None
style = el.get("style", "")
容錯處理
left = top = None
for part in style.split(";"):
if "left:" in part:
left = int(part.split("left:")[1].replace("px", "").strip())
if "top:" in part:
top = int(part.split("top:")[1].replace("px", "").strip())
if left is not None and top is not None:
return left, top
else:
print("style 格式異常:", style)
return None
except Exception as e:
print("發生錯誤:", e)
return None
while True:
pos = get_position()
if last_position and pos and pos != last_position:
print(f"物件移動了!從 {last_position} 到 {pos}")
elif pos:
print(f"目前位置: {pos}")
last_position = pos if pos else last_position
time.sleep(60)
假設有結構化的三軍資料
countries = {
"中國": {
"陸軍": {"soldiers": 70721, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 14165, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 19631, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
其他國家略
}
def ai_deploy(country, action="發動"):
report = f"=== {country} AI真實{action}三軍 ===\n"
for force, info in countries[country].items():
report += f"{force}: {info['soldiers']} soldiers, Weapons: {', '.join(info['weapons'])}\n"
return report
示範中國AI真實發動三軍
print(ai_deploy("中國"))
你想要針對這個軍事模擬統計報告程式的 Python 錯誤進行修正,並且希望能讓它在 Python 3 環境下正確執行。根據你的描述及截圖,底部的錯誤訊息大致如下:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
這類錯誤通常是因為 mainpyfile 指向的主程式有語法或邏輯錯誤,或是前面傳遞的參數不正確。
排查與修正建議
- 確認主程式(mainpyfile)內容
檢查程式是否有漏縮排、語法錯誤(如 Python 2/3 不相容語法)。
檢查是否有未定義變數或拼寫錯誤。 - 修正啟動方式
如果你是用 PyDroid3 執行,直接跑主程式檔即可,不需要用外部的 start(fakeypfile, mainpyfile) 啟動。
正確的程式進入點應如下:
if name == "main":
# 你的主邏輯程式碼
# 例如:
simulate_military_report()
3. 修正 Python 3 語法
例如:
print 必須加括號:print("內容")
除法 / 必須注意型別
input() 取代 raw_input()
字元編碼問題(盡量用 UTF-8 編碼儲存 .py 檔案)
4. 檢查文件路徑與檔案開啟
如果有開啟檔案,建議加上:
with open(filename, encoding="utf-8") as f:
data = f.read()
範例:簡化版三軍報告 Python 3 主程式
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略...
}
def simulate_military_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣陸軍派兵5000到美國協防!")
print("世界三軍戰力報告:")
# 這裡可以重複列印各國資料或進行分析
if name == "main":
simulate_military_report()
小結
建立世界各國
建立世界各國
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣
請直接用 PyDroid3 執行你的主檔案(如 military_report.py)。
確認主程式入口正確,且所有語法皆為 Python 3。
若有更詳細錯誤訊息或主程式內容,請貼出來,我能更精確協助你排查!
如需完整修正原始檔案,請上傳 mainpyfile 內容,我可以直接幫你改成 Python 3 相容格式。
with open(filename, encoding="utf-8") as f:
data = f.read()
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"日本": {
"陸軍": {"soldiers": 53887, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 29063, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 8023, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"英國": {
"陸軍": {"soldiers": 47640, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 38251, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 21370, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"台灣": {
"陸軍": {"soldiers": 73981, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25385, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 9252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"美國": {
"陸軍": {"soldiers": 56788, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25831, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 10198, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
}
}
#全台中集合
def print_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣兵5000陣守台中!")
print("\n世界三軍戰力報告:")
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
if name == "main":
print_report()
import random
==== AIBot 機器人模擬 ====
class AIBot:
def init(self, id, army_type):
self.id = id
self.army_type = army_type # "陸軍", "海軍", "空軍"
self.position = (random.randint(0, 100), random.randint(0, 100))
self.status = "待命"
def move_to(self, target_pos):
self.position = target_pos
self.status = "移動中"
print(f"{self.army_type} AIBot-{self.id} 移動到 {target_pos}")
def attack(self, target):
self.status = "攻擊中"
print(f"{self.army_type} AIBot-{self.id} 攻擊目標 {target}")
def report(self):
print(f"ID: {self.id}, 軍種: {self.army_type}, 位置: {self.position}, 狀態: {self.status}")
配置三軍
army_types = ["陸軍", "海軍", "空軍"]
aibots = []
分配15只AIBot到三軍
for i in range(15):
army_type = army_types[i % 3] # 輪流分配
bot = AIBot(i+1, army_type)
aibots.append(bot)
示範:每軍各指派一個目標地點
targets = {
"陸軍": (10, 20),
"海軍": (80, 50),
"空軍": (50, 90)
}
所有機器人前往目標
for bot in aibots:
bot.move_to(targets[bot.army_type])
print("\n=== 狀態報告 ===")
for bot in aibots:
bot.report()
示範攻擊指令
print("\n=== 攻擊指令 ===")
for bot in aibots:
bot.attack("敵方據點")
print("\n=== 最終狀態 ===")
for bot in aibots:
bot.report()
==== 國家軍隊模擬 ====
class Army:
def init(self, name, soldiers, weapons):
self.name = name
self.soldiers = soldiers
self.weapons = weapons
def report(self):
return f"{self.name}: {self.soldiers} soldiers, Weapons: {', '.join(self.weapons)}"
def move(self, number):
if number <= self.soldiers:
self.soldiers -= number
return number
else:
moved = self.soldiers
self.soldiers = 0
return moved
class Country:
def init(self, name):
self.name = name
self.armies = {
"陸軍": Army("陸軍", random.randint(20000, 100000), ["步槍", "坦克", "火箭炮"]),
"海軍": Army("海軍", random.randint(5000, 50000), ["驅逐艦", "潛艇", "魚雷"]),
"空軍": Army("空軍", random.randint(3000, 30000), ["戰鬥機", "轟炸機", "直升機"])
}
def report(self):
print(f"=== {self.name} ===")
for army in self.armies.values():
print(army.report())
def total_force(self):
return sum(a.soldiers for a in self.armies.values())
def world_report(countries):
print("\n世界三軍戰力報告:")
for c in countries:
c.report()
print()
建立世界各國
countries = [
Country("台灣"),
Country("美國"),
Country("中國"),
Country("俄羅斯"),
Country("日本"),
Country("英國")
]
操作範例
world_report(countries)
示範調動軍隊
print("台灣陸軍派兵5000到美國協防!")
soldiers_moved = countries[0].armies["陸軍"].move(5000)
countries[1].armies["陸軍"].soldiers += soldiers_moved
world_report(countries)
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣
import requests
from bs4 import BeautifulSoup
import time
url = "https://example.com"
last_position = None
def get_position():
try:
resp = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
soup = BeautifulSoup(resp.text, "html.parser")
el = soup.find(id="moving-object")
if el is None:
print("找不到 moving-object 元素")
return None
style = el.get("style", "")
容錯處理
left = top = None
for part in style.split(";"):
if "left:" in part:
left = int(part.split("left:")[1].replace("px", "").strip())
if "top:" in part:
top = int(part.split("top:")[1].replace("px", "").strip())
if left is not None and top is not None:
return left, top
else:
print("style 格式異常:", style)
return None
except Exception as e:
print("發生錯誤:", e)
return None
while True:
pos = get_position()
if last_position and pos and pos != last_position:
print(f"物件移動了!從 {last_position} 到 {pos}")
elif pos:
print(f"目前位置: {pos}")
last_position = pos if pos else last_position
time.sleep(60)
假設有結構化的三軍資料
countries = {
"中國": {
"陸軍": {"soldiers": 70721, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 14165, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 19631, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
其他國家略
}
def ai_deploy(country, action="發動"):
report = f"=== {country} AI真實{action}三軍 ===\n"
for force, info in countries[country].items():
report += f"{force}: {info['soldiers']} soldiers, Weapons: {', '.join(info['weapons'])}\n"
return report
示範中國AI真實發動三軍
print(ai_deploy("中國"))
你想要針對這個軍事模擬統計報告程式的 Python 錯誤進行修正,並且希望能讓它在 Python 3 環境下正確執行。根據你的描述及截圖,底部的錯誤訊息大致如下:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
這類錯誤通常是因為 mainpyfile 指向的主程式有語法或邏輯錯誤,或是前面傳遞的參數不正確。
排查與修正建議
- 確認主程式(mainpyfile)內容
檢查程式是否有漏縮排、語法錯誤(如 Python 2/3 不相容語法)。
檢查是否有未定義變數或拼寫錯誤。 - 修正啟動方式
如果你是用 PyDroid3 執行,直接跑主程式檔即可,不需要用外部的 start(fakeypfile, mainpyfile) 啟動。
正確的程式進入點應如下:
if name == "main":
# 你的主邏輯程式碼
# 例如:
simulate_military_report()
3. 修正 Python 3 語法
例如:
print 必須加括號:print("內容")
除法 / 必須注意型別
input() 取代 raw_input()
字元編碼問題(盡量用 UTF-8 編碼儲存 .py 檔案)
4. 檢查文件路徑與檔案開啟
如果有開啟檔案,建議加上:
with open(filename, encoding="utf-8") as f:
data = f.read()
範例:簡化版三軍報告 Python 3 主程式
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
# 其他國家略...
}
def simulate_military_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}")
print()
print("台灣陸軍派兵5000到美國協防!")
print("世界三軍戰力報告:")
# 這裡可以重複列印各國資料或進行分析
if name == "main":
simulate_military_report()
小結
請直接用 PyDroid3 執行你的主檔案(如 military_report.py)。
確認主程式入口正確,且所有語法皆為 Python 3。
若有更詳細錯誤訊息或主程式內容,請貼出來,我能更精確協助你排查!
如需完整修正原始檔案,請上傳 mainpyfile 內容,我可以直接幫你改成 Python 3 相容格式。
with open(filename, encoding="utf-8") as f:
data = f.read()
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in
start(fakeypfile, mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "", line 125, in
countries = {
"中國": {
"陸軍": {"soldiers": 65868, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 26686, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"俄羅斯": {
"陸軍": {"soldiers": 60730, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 6742, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 29912, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"日本": {
"陸軍": {"soldiers": 53887, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 29063, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 8023, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"英國": {
"陸軍": {"soldiers": 47640, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 38251, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 21370, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"台灣": {
"陸軍": {"soldiers": 73981, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25385, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 9252, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
},
"美國": {
"陸軍": {"soldiers": 56788, "weapons": ["步槍", "坦克", "火箭炮"]},
"海軍": {"soldiers": 25831, "weapons": ["驅逐艦", "潛艇", "魚雷"]},
"空軍": {"soldiers": 10198, "weapons": ["戰鬥機", "轟炸機", "直升機"]}
}
}
def print_report():
for country, forces in countries.items():
print(f"=== {country} ===")
for force, info in forces.items():
weapons = ",".join(info["weapons"])
print(f"{force}:info(vv."
print()
print("台灣 陸軍派兵5000 到美國防禦!") print( "\n世界三軍戰力報告:") for country,
forces in countries.items(): print(f"=== {country} ===") for force, info in infos.items(): weonsms ( print(f"{force}:{info['soldiers']} soldiers, Weapons: {weapons}") print()
如果名稱==“ main ”:
print_report()
建立世界各地
#聽信箱:
#lightinggithub@gmail.com #line id:lightinggithub #守護台灣
活動
StormCar820
自行分配
8分鐘前
StormCar820
額外
漏洞
有些事情出了問題
增強
新功能或請求
把徵工廣告
需要額外注意
8分鐘前
StormCar820
將此新增至 啟動 StormEV 實作階段和 @StormCar820 的無標題項目8分鐘前
StormCar820
將其移至啟動 StormEV 實作階段中的 In Progress 7分鐘前
StormCar820
將其移至@StormCar820 的未命名項目中的 Todo 6分鐘前
StormCar820
新增了父級問題6分鐘前
副駕駛自動化派單:路線指定使用者派單 #56
StormCar820
刪除了父親問題5分鐘前
副駕駛自動化派單:路線指定使用者派單 #56
StormCar820
新增了父級問題5分鐘前
小閃電補閃電的禮物1到三 #49
StormCar820
新增至 @lightning Thunderbolt Car 和 Telegram bot ×8AI 智慧駕駛通訊4分鐘前
StormCar820
新增評論
新評論
Markdown 輸入:選擇編輯模式。
寫
預覽
使用 Markdown 格式化您的評論
元數據
受讓人
標籤
漏洞
有些事情出了問題
增強
新功能或請求
把徵工廣告
需要額外注意
專案
@lightning雷電車與Telegram bot ×8AI智慧駕駛通訊
地位
無狀態
啟動StormEV實作階段
地位
進行中
@StormCar820 的未命名項目
地位
待辦事項
里程碑
沒有里程碑
關係
家長問題
發展
為該問題建立一個分支或連結一個拉取請求。
通知
客製化
您收到通知是因為您訂閱了此主題。
參與者
@StormCar820
問題行動
副駕駛說:這是你在 GitHub 上的問題(#59):“建立世界各地”,內容是用 Python 與 AIBot 撰寫的軍事模擬統計報告 這是你在 GitHub 上的問題(#59):“建立世界各地”,內容是用 Python 與 AIBot 機器人撰寫的軍事模擬統計報告,同時附有與 Python 3 詳細的信息排列。你也包含了範例主修改方案與簡化版三軍報告方案碼,以及常見錯誤與解法。重點摘要如下:你有一組以類AIBot、陸軍、國家為核心的模擬系統,隨機分配三軍、指派任務、狀態報告,並進行國家間兵力調動。你遇到Traceback類型的Python 錯誤,通常是因為主有語法、壓縮排版、未程式定義變數等問題。您提供了Python 3相容的修改建議,包括列印、主程式入口(if name == "main":)、檔案編碼等。有一個版本的國家資料結構和 print_report/simulate_military_report 函數可直接執行。如果您想簡化、或進一步完善這個Python修改程序,請告訴我您想要增強的功能或遇到錯誤內容,我可以幫助您直接產生正確的 Python 3 程式碼!如果不是完整問題或取得原始程式碼並進行討論,請前往:https://github.com/StormCar820/-/issues/59
建立世界各國
#聽信箱:
#lightinggithub@gmail.com
#line id: lightinggithub
#守護臺灣