我试图做一个游戏,用户被要求猜测此基础上,一个国家的其中随机选择从字典(在底部类似的链接)的列表资本。
猜共10个国家,如果他们猜对了,他们拿到1分,在总共10个点。
我已经导入的变量“国家”包含类似下面的词典列表:
[{'capital': 'Andorra la Vella',
'code': 'AD',
'continent': 'Europe',
'name': 'Andorra',
'timezones': ['Europe/Andorra']},
{'capital': 'Kabul',
'code': 'AF',
'continent': 'Asia',
'name': 'Afghanistan',
'timezones': ['Asia/Kabul']},
那么,如何从打印一个特定的键名随机选择呢?在这种情况下,从任何字典中的任何“资本”。
random.choice
是这种使用情况非常好:)
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))
您可以使用以下两种选择。
示例代码。
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")
示例代码:
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")
您可以取得与randomCountry = random.choice(countries)
随机样本
但是,如果你这样做多次,你可以多次获得同一个国家。为了解决这个问题,你可以品尝与randomCountries = random.sample(countries, 10)
10个不同的元素,然后用这些迭代。
需要注意的是,如果你试图超过集合中存在品尝更多的元素random.sample
抛出一个错误。
因此,你的游戏看起来是这样的:
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")
像这样?
import random
place_list = [{'capital': 'Andorra la Vella', 'code': 'AD', 'continent': 'Europe', 'name': 'Andorra', 'timezones': ['Europe/Andorra']}, {'capital': 'Kabul', 'code': 'AF', 'continent': 'Asia', 'name': 'Afghanistan', 'timezones': ['Asia/Kabul']}]
quiz_length = 10
points = 0
for q in random.sample(place_list, quiz_length):
guess = input(f'this place has {q['capital']} in it')
if guess == q['name']:
points += 1
print(f'you got {points}/{quiz_length}')
编辑:该代码的其余部分...