我正在制作一個機器人互相打架的小游戲。機器人在資料庫中有一個表格,其中記錄了他們的比賽分數。每個機器人都有自己的家鄉島,可以在那里面對對手。對手將擁有自己的島嶼。每個機器人都有自己的島嶼。問題很簡單,但可能會引起混淆,所以我一步一步簡單解釋一下:
解釋
1.我從組合框中選擇特定機器人的名稱
if robot_her_island:
select_robot_on_her_island = select_war.split('-')[0]
2.使用Last_War_on_any_island我搜索所選機器人的最后一場戰爭。我尋找發生在你自己島上或你對手島上的戰爭。我只選最后一戰
#Last War on ANY island
cursor.execute("SELECT robot_on_her_island, robot_on_island_away, points_war_home, points_war_away FROM War WHERE robot_on_her_island =? OR robot_on_island_away=? LIMIT 1", [select_robot_on_her_island, select_robot_on_her_island])
Last_War_on_any_island = cursor.fetchall()[0]
3.我創建了兩個單獨的 cursor.execute:一個用于機器人在自己的島嶼上戰斗,另一個用于機器人在非自己的島嶼上戰斗。怎么來的?因為問題在第2點看起來是這樣解決的,但是只有在robot_on_her_islandVS robot_on_island_away(資料庫中的兩個資料)之間發生戰爭時才可以,然后對應的點是points_war_home和points_war_away。但是當戰爭倒退時,即robot_on_island_awayVS robot_on_her_island,那么我也應該反轉points_war_away- points_war_home,因為我正在嘗試獲取有關某個機器人的資訊(在初始組合框中選擇)。
因此,為了解決這個問題,我考慮創建兩個單獨的 cursor.execute,一個用于機器人在自己的島嶼上戰斗時 ( Last_War_on_her_island),另一個用于機器人在不是它自己的島嶼上戰斗時 ( Last_War_on_island_away)。這樣我也可以交換points_war_home和points_war_away(相反,通過顛倒順序)
我舉第3點的例子:
MAZINGER VS GOLDRAKE robots_on_her_island VS robots_on_island_away = points_war_home, points_war_away (Mazinger, Goldrake = 2-1)
GOLDRAKE VS MAZINGER robots_on_her_island VS robots_on_island_away = points_war_away, points_war_home (Goldrake-Mazinger = 3-2) #invert points
#Last War in robot home island
cursor.execute("SELECT points_war_home, points_war_away FROM War WHERE robot_on_her_island =? LIMIT 1", [select_robot_on_her_island])
Last_War_on_her_island = cursor.fetchall()[0]
#Last War in robot away island (robot island opponents)
cursor.execute("SELECT points_war_away, points_war_home FROM War WHERE robot_on_island_away =? LIMIT 1", [select_robot_on_her_island])
Last_War_on_island_away= cursor.fetchall()[0]
好的!現在我有最后的robot_on_her_island 戰爭,以及最后的機器人對手robot_on_island_away 戰爭。完美,但問題就在這里!
問題
我怎么知道我最初從組合框中選擇的最后一次機器人大戰是在它自己的島上還是在其他機器人的島上進行的?
答案是我上面用的cursor.execute叫Last_War_on_any_island,但是不知道怎么把代碼放在一起。讓我解釋一下:有Last_War_on_any_islandwhich 用于找出我用組合框選擇的機器人的最后一次一般戰爭是什么,然后使用Last_War_on_any_island我希望能夠得到Last_War_on_her_island或Last_War_on_island_away
例如我想得到這個:

For example, if I choose Daitarn in the combobox, the last war of Daitarn will be sought, which is the one in id 2 (Jeeg-Daitarn). Then Last_War_on_island_away will be printed. While if I had chosen Mazinger in the combobox, Mazinger's last war would have been in id 1 (Mazinger-Goldrake) and therefore Last_War_on_her_island would have been printed. IMPORTANT: When I say "last" I refer to the last time the robot NAME is displayed in the tab (indifferently from Home or Away, or ID), but only the last time the name is present
CODE COMPLETE
if robot_her_island:
robot_her_island = select_war.split('-')[0]
#Last War on ANY island
cursor.execute("SELECT robot_on_her_island, robot_on_island_away, points_war_home, points_war_away FROM War WHERE robot_on_her_island =? OR robot_on_island_away=? LIMIT 1", [select_robot_on_her_island, select_robot_on_her_island])
Last_War_on_any_island = cursor.fetchall()[0]
#Last War in robot home island
cursor.execute("SELECT points_war_home, points_war_away FROM War WHERE robot_on_her_island =? LIMIT 1", [select_robot_on_her_island])
Last_War_on_her_island = cursor.fetchall()[0]
#Last War in robot away island (robot island opponents)
cursor.execute("SELECT points_war_away, points_war_home FROM War WHERE robot_on_island_away =? LIMIT 1", [select_robot_on_her_island])
Last_War_on_island_away= cursor.fetchall()[0]
#THE PROBLEM IS HERE
if Last_War_on_any_island is Last_War_on_her_island:
print(Last_War_on_her_island)
else
print(Last_War_on_island_away)
uj5u.com熱心網友回復:
您可以在查詢本身中測驗它是在家還是在外。
cursor.execute("""
SELECT
CASE WHEN robot_on_her_island = ? THEN 'Last_War_on_her_island'
ELSE 'Last_War_on_island_away'
END AS which_island,
points_war_home, points_war_away
FROM war
WHERE ? IN (robot_on_her_island, robot_on_island_away)
LIMIT 1""",
(select_robot_on_her_island, select_robot_on_her_island))
row = cursor.fetchone()
if row:
which_island, points_home, points_away = row
if which_island == 'Last_War_on_her_island':
print(which_island, points_home, points_away)
else:
# Swap the points when won on the away island
print(which_island, points_away, points_home)
順便說一句,如果您只想要一行,請使用fetchone()而不是fetchall()[0].
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/401098.html
