[英]How do I print an item and the list it came from in Python?
我正在嘗試創建輪盤賭模擬器,當前該程序僅打印號碼。 如何獲得打印號碼及其所在列表標題的信息?
例:
import random
red=[1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36]
black=[2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35]
spin=random.randint(0,36)
print(spin)
if spin in red:
print('{} Red'.format(spin))
elif spin in black:
print('{} Black'.format(spin))
else:
print('{}'.format(spin))
您只需要檢查兩個條件,要么為0
,要么為紅色或黑色,如果不為0且不是紅色,則必須為黑色
import random
red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
spin = random.randint(0, 36)
print("{} {}".format(spin, ["black","red"][spin in red] if spin != 0 else "green"))
明確寫出的內容變為:
red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
spin = random.randint(0, 36)
if spin == 0:
print("green 0")
elif spin in red:
print("red {}".format(spin))
else:
print("black {}".format(spin))
數字黑名單無關緊要。
如果要表示00
,則需要使用字符串而不是整數,因為前導0表示python 2中為八進制,而在python3中為無效語法
import random
red = {'25', '12', '14', '16', '19', '32', '30', '23', '36', '34', '1', '3', '27', '5', '7', '18', '9', '21'}
nums = ["00",'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35']
spin = random.choice(nums)
if spin in {"0","00"}:
print("green {}".format(spin))
elif spin in red:
print("red {}".format(spin))
else:
print("black {}".format(spin))
您還可以使用字典映射數字為顏色着色並選擇一個隨機鍵:
d = {'00':"green",'0':"green",'24': 'black', '25': 'red', '26': 'black', '27': 'red', '20': 'black',
'21': 'red', '22': 'black', '23': 'red', '28': 'black', '29': 'black', '1': 'red', '3': 'red',
'2': 'black', '5': 'red', '4': 'black', '7': 'red', '6': 'black', '9': 'red', '8': 'black',
'11': 'black', '10': 'black', '13': 'black', '12': 'red', '15': 'black', '14': 'red', '17':
'black', '16': 'red', '19': 'red', '18': 'red', '31': 'black', '30': 'red', '36': 'red', '35':
'black', '34': 'red', '33': 'black', '32': 'red'}
spin = random.choice(list(d))
print("{} {}".format(d[spin],spin))
如果要獲取百分比,則可以運行n
試驗並使用Counter dict保持計數:
nums = list(d)
from collections import Counter
num_count = Counter()
colours = Counter()
for test in range(10000):
spin = random.choice(nums)
num_count[spin] += 1
colours[d[spin]] += 1
for k,v in colours.most_common():
print("{} rolled {} times which is {}% of the total spin".format(k,v, v / 10000.0 * 100))
print(" ")
for k,v in num_count.most_common():
print("{} rolled {} times which is {}% of the total spins".format(k,v, v / 10000.0 * 100))
輸出:
black rolled 4748 times which is 47.48% of the total spin
red rolled 4726 times which is 47.26% of the total spin
green rolled 526 times which is 5.26% of the total spin
20 rolled 301 times which is 3.01% of the total spins
36 rolled 296 times which is 2.96% of the total spins
23 rolled 292 times which is 2.92% of the total spins
3 rolled 285 times which is 2.85% of the total spins
00 rolled 284 times which is 2.84% of the total spins
13 rolled 282 times which is 2.82% of the total spins
12 rolled 280 times which is 2.8% of the total spins
27 rolled 278 times which is 2.78% of the total spins
15 rolled 273 times which is 2.73% of the total spins
14 rolled 272 times which is 2.72% of the total spins
6 rolled 270 times which is 2.7% of the total spins
8 rolled 270 times which is 2.7% of the total spins
2 rolled 268 times which is 2.68% of the total spins
5 rolled 267 times which is 2.67% of the total spins
35 rolled 267 times which is 2.67% of the total spins
19 rolled 266 times which is 2.66% of the total spins
34 rolled 265 times which is 2.65% of the total spins
33 rolled 265 times which is 2.65% of the total spins
21 rolled 264 times which is 2.64% of the total spins
4 rolled 264 times which is 2.64% of the total spins
24 rolled 263 times which is 2.63% of the total spins
17 rolled 262 times which is 2.62% of the total spins
31 rolled 261 times which is 2.61% of the total spins
11 rolled 260 times which is 2.6% of the total spins
18 rolled 257 times which is 2.57% of the total spins
29 rolled 255 times which is 2.55% of the total spins
30 rolled 253 times which is 2.53% of the total spins
10 rolled 251 times which is 2.51% of the total spins
22 rolled 248 times which is 2.48% of the total spins
28 rolled 247 times which is 2.47% of the total spins
1 rolled 245 times which is 2.45% of the total spins
25 rolled 243 times which is 2.43% of the total spins
32 rolled 243 times which is 2.43% of the total spins
0 rolled 242 times which is 2.42% of the total spins
9 rolled 242 times which is 2.42% of the total spins
26 rolled 241 times which is 2.41% of the total spins
7 rolled 240 times which is 2.4% of the total spins
16 rolled 238 times which is 2.38% of the total spins
基本上,您可以使用“ in”運算符進行搜索。
if spin in red:
//do for red
elif spin in black:
//do for black...
else:
//take a spin again
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.