Python簡單函式迴圈綜合例項

leagueandlegends發表於2024-04-05
import random

print("*"*71)
print("*"*27+"歡迎來到名人猜猜猜"+"*"*27)
print("*"*29+"Let's begining"+"*"*28)
character_1='他是巨星'
character_2='他是籃球健將'
character_3='他身高超過2米'
character_4='他愛國'
character_5='他是北京申辦冬奧會形象大使之一'
character_6='他是NBA球星麥迪的隊友'
character_7='他喜歡做慈善'
character_8='他是上海人'
character_9='汶川地震他捐款1600萬'
character_10='他是中國人'
the_man='姚明'

characters=['character_1','character_2','character_3','character_4',
           'character_5','character_6','character_7','character_8',
           'character_9','character_10']
def random_Int(start,end):
    return random.randint(start,end-1)
length=len(characters)
count=0
while len(characters)>0:
    count=count+1
    index=random_Int(0,len(characters))
    print("請輸入特性%d" %count,eval(characters[index]))
    guess_name=input("請輸入猜測的人名:")
    if guess_name==the_man:
        print("您猜對了")
        break
    else:
        characters.pop(index)
        if count==10:
            print("您猜錯了十次")
            break
        else:
            print("請繼續猜")

相關文章