第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python 3:如何使用用戶輸入并從字典列表中檢索答案制作游戲,然后使用積分系統(tǒng)?

Python 3:如何使用用戶輸入并從字典列表中檢索答案制作游戲,然后使用積分系統(tǒng)?

慕標(biāo)琳琳 2021-11-02 17:14:08
我正在嘗試制作一個(gè)游戲,要求用戶根據(jù)從字典列表中隨機(jī)選擇的首都來猜測(cè)一個(gè)國(guó)家(類似于底部的鏈接)。一共猜10個(gè)國(guó)家,猜對(duì)了得1分,一共10分。我導(dǎo)入了一個(gè)變量“countries”,其中包含如下所示的字典列表:[{'capital': 'Andorra la Vella',  'code': 'AD',  'continent': 'Europe',  'name': 'Andorra',  'timezones': ['Europe/Andorra']}, {'capital': 'Kabul',  'code': 'AF',  'continent': 'Asia',  'name': 'Afghanistan',  'timezones': ['Asia/Kabul']},那么如何從特定的鍵名中打印隨機(jī)選擇呢?在這種情況下,任何字典中的任何“大寫”。
查看完整描述

3 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

random.choice 非常適合這個(gè)用例:)


import random



country_dlist = [{'capital': 'Andorra la Vella',

  'code': 'AD',

  'continent': 'Europe',

  'name': 'Andorra',

  'timezones': ['Europe/Andorra']},

 {'capital': 'Kabul',

  'code': 'AF',

  'continent': 'Asia',

  'name': 'Afghanistan',

  'timezones': ['Asia/Kabul']}

 ]


def run():

    tot_points = 0

    num_of_ques = len(country_dlist)

    for i in range(num_of_ques):

        choice = random.choice(country_dlist)

        que = country_dlist.remove(choice)

        capital = raw_input("Please enter the captial for country {}: ".format(choice['name']))

        if capital.lower() == choice['capital'].lower(): # case insensitive match :)

            tot_points += 1

    return tot_points


points = run()

print("You scored {} points".format(points))


查看完整回答
反對(duì) 回復(fù) 2021-11-02
?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊

您可以使用以下兩個(gè)選項(xiàng)。


random.choice從列表中選擇一個(gè)隨機(jī)元素。

示例代碼。


from random import choice

country_dict = [{'capital': 'Andorra la Vella',     'code': 'AD',  continent': 'Europe',      'name': 'Andorra',      'timezones': 'Europe/Andorra']},

                {'capital': 'Kabul',      'code': 'AF',      'continent': 'Asia',      ame': 'Afghanistan',      'timezones': ['Asia/Kabul']}

               ]

country = choice(country_dict)

capital = input("Please enter the captial for country "+country['name'])

if capital == country['capital']:

    print("Correct answer")

random.ranint選擇 0 和列表長(zhǎng)度之間的隨機(jī)整數(shù)。

示例代碼:


from random import randint

country_dict = [{'capital': 'Andorra la Vella',      'code': 'AD',      'continent': 'Europe',      'name': 'Andorra',      'timezones': ['Europe/Andorra']},

                {'capital': 'Kabul',      'code': 'AF',      'continent': 'Asia',      'name': 'Afghanistan',      'timezones': ['Asia/Kabul']}

               ]

ind = randint(0,len(country_dict)-1)

capital = input("Please enter the captial for country "+country_dict[ind]['name'])

if capital == country_dict[ind]['capital']:

    print("Correct answer")


查看完整回答
反對(duì) 回復(fù) 2021-11-02
?
冉冉說

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

您可以使用以下方法獲取隨機(jī)樣本 randomCountry = random.choice(countries)


但是,如果您多次執(zhí)行此操作,您可能會(huì)多次獲得同一個(gè)國(guó)家/地區(qū)。為了解決這個(gè)問題,您可以對(duì) 10 個(gè)不同的元素進(jìn)行采樣randomCountries = random.sample(countries, 10),然后使用它們進(jìn)行迭代。


請(qǐng)注意,random.sample如果您嘗試對(duì)比集合中存在的元素更多的元素進(jìn)行采樣,則會(huì)引發(fā)錯(cuò)誤。


因此,您的游戲可能如下所示:


import random


countries = [

    {'capital': 'Andorra la Vella', 'code': 'AD', 'continent': 'Europe', 'name': 'Andorra', 'timezones': ['Europe/Andorra']}, 

    {'capital': 'Kabul', 'code': 'AF', 'continent': 'Asia', 'name': 'Afghanistan', 'timezones': ['Asia/Kabul']},

    ...

]


rounds = 10

random_countries = random.sample(countries, rounds) # returns 10 random elements (no duplicates)


score = 0

for country in random_countries:

    print("Score: %d / %d | Which country has the capital: %s?" % (score, rounds, country['capital']))

    country_response = input()

    if country_response == country['name']:

        score += 1

        print("Correct")

    else:

        print("Incorrect")


查看完整回答
反對(duì) 回復(fù) 2021-11-02
  • 3 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)