我有這樣一段代碼,當你猜對答案時,它會回圈到相同的原始隨機選擇。我需要能夠選擇我認為有更多追隨者的選項,如果它是正確的,就回圈回到兩個完全不同的隨機選項,而不是第一個。
import game_data
import random
import art
names = random.choice(game_data.data)
names2 = random.choice(game_data.data)
correct_score = int()
def game(name,names2,correct_score)。
#print(art.logo)。
follower_count = names[' follower_count']
print(f "Compare A。{names['name']}, a {names['description']}, 從{names['country']}。 ")
#print(art.vs)
follower_count2 = names2[' follower_count']
print(f "反對B。{names2['name']}, a {names2['description']}, 從{names2['country']}。 ")
answer = input("Who has more followers 'A' or 'B': ").lower()
if answer == 'a' and follower_count > follower_count2:
print("correct")
correct_score = 1 "Correct"
print(correct_score)。
game(name=names,names2=names2,correct_score=correct_score)
elif answer == 'b' and follower_count2 > follower_count:
print("correct")
correct_score = 1 "Correct"
print(correct_score)。
game(name=names,names2=names2,correct_score=correct_score)
game(name=names,names2=names2,correct_score=correct_score)
下面是我其他檔案的代碼樣本。
data = [
{
'name': 'Instagram',
'follower_count': 346,
'description': '社交媒體平臺'。
'國家': 'United States'。
},
{
'name': 'Cristiano Ronaldo',
' follower_count': 215,
'description': '足球運動員',
'國家': '葡萄牙'。
},
{
'name': 'Ariana Grande',
' follower_count': 183,
'description': 'Musician and actress',
'國家': 'United States'。
},
{
'name': 'Dwayne Johnson',
' follower_count': 181,
'description': 'Actor and professional wrestler',
'國家': 'United States'。
},
uj5u.com熱心網友回復:
遞回像這樣要求兩件事:
首先,你的演算法必須有一個終止條件。也就是說,必須有一種方法讓你的函式在不呼叫自己的情況下終止。
第二,你的演算法必須傳遞一個不同的狀態(引數)來再次呼叫自己,這樣它就可以進一步做出決定來終止遞回。如果狀態(引數)保持不變,你怎么能決定它何時完成?
這就是了! 這正是你需要考慮的兩件事。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/325710.html
標籤:
上一篇:如何將資料添加到物件的串列中?
