简体   繁体   English

Python 列表元素组合

[英]Python combinations of list elements

I have the following code:我有以下代码:

from random import randint 
Roll=[]
for i in range(3):
    zari=randint(1,6)
    Roll.append(zari)
for b in Roll:

the above code is deficient.上面的代码是有缺陷的。
What i want my code to do is if inside the Roll list is the combination of numbers 4,5,6 to print('Player won') but im kinda lost我想要我的代码做的是,如果 Roll 列表中是要print('Player won') ,但我有点输了
Any suggestions?有什么建议么?

The player wins if the combinations of the numbers is 4,5,6 and he loses if the combination is 1,2,3.This is the code that i managed to make but i cant figure out the rest of it如果数字组合为 4,5,6,则玩家获胜,如果组合为 1,2,3,则玩家输。这是我设法制作的代码,但我无法弄清楚它的 rest
Also i forgot to mention that player wins if the combination is 2 same numbers followed by the number 6 for example 2,2,6.我也忘了提到如果组合是 2 个相同的数字后跟数字 6,例如 2、2、6,玩家获胜。 Any ideas on this?对此有什么想法吗?
Thank you for every answer谢谢你的每一个回答

Try by comparing your result (Roll) with the set {4,5,6}:尝试将您的结果(滚动)与集合 {4,5,6} 进行比较:

if set(Roll)=={4,5,6}:
    print('Player won')

Full code:完整代码:

from random import randint 
Roll=[]
for i in range(3):
    zari=randint(1,6)
    Roll.append(zari)
if set(Roll)=={4,5,6}:
    print('Player won')

If I understood correctly, you want to check at any given time if Roll contains 4, 5 and 6. In that case, just do this: 4 in Roll and 5 in Roll and 6 in Roll , which will return a Boolean如果我理解正确,您想在任何给定时间检查Roll是否包含 4、5 和 6。在这种情况下,只需执行以下操作: 4 in Roll and 5 in Roll and 6 in Roll ,这将返回 Boolean

Its a game called Cee-lo .它是一款名为Cee-lo的游戏。

Check out my code:查看我的代码:

from random import randint

while(True):
    Roll=[]
    for i in range(3):
        zari=randint(1,6)
        Roll.append(zari)
    if set(Roll)=={4,5,6}:
        print('Player won');   break
    elif set(Roll)=={1,2,3}:
        print('Player lose');  break
# If Roll has two same values, set() will remove duplicate value
    elif len(set(Roll))==2 and 6 in Roll:
        print('Player won',Roll);   break
    else:
        print('Rolling the dice again')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM