Skip to content
Advertisement

I am stuck with this for loop problem in python [closed]

I am trying to make a a program to calculate the probability of a game. So the game is like the roulette but it has only four colors and no numbers( Red, Blue, Purple, and green). So the program’s idea is every time the dealer’s rotate the roulette I write down the color and so on. It is easy to find the color’s probability but I was trying to see if there is a sequence and if there is I want the program to tell me what is the next color in that sequence so I could bet on it. for example, if I got before G,G,P,R,G and then got R, I would assume every time I get this sequence I would have the same result, R. This is my code. I am pretty sure there is something wrong with my smiler_lists function

sequence= [4, 2, 2, 2, 2, 3, 1, 2, 4, 2, 2, 2, 2, 3, 1, 2, 4, 3, 1, 3,
1, 1, 2, 3, 4, 4, 3, 4, 3, 1, 3, 4, 2, 2 , 3, 3, 1, 4, 1, 2, 1, 3,2,
2, 1, 2, 1, 3, 2, 2, 3, 3, 2, 2, 3, 2, 1, 2, 1, 2, 3, 4, 2, 4, 2, 1,
1, 1, 1, 3, 4, 2, 3, 4, 4, 3, 2, 2, 1, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2,
2, 2, 4, 2, 2, 2, 1, 2, 4, 3, 2, 2, 2, 2, 2, 4,4,2] 
purble =0 
green =0
blue =0 
red =0

def program( ):
    x=0
    global sequence,purble, green ,blue  ,red
    s= int(input("whats the resut? 1= Red , 2= purble , 3=blue , 4= green,  "))
    
    if s == 4:
        green +=1
        sequence.append(s)
    elif s == 2:
        purble +=1
        sequence.append(s)
    elif s == 3:
        blue +=1
        sequence.append(s)
    elif s == 1 :
        red +=1
        sequence.append(s)
    elif s == 0:
        x+=1
    else:
        print("wrong entery" )
        program()
    total = (green + red + blue + purble)
    print("Green probability ={:.4f}nRed probability ={:.4f}n Blue probability ={:.4f}n Purbule probability ={:.4f}n The total = {}n"
          .format(green*100/total,red*100/total,blue*100/total,purble*100/total,total))
    if len(sequence) >= 5:
        set2= sequence[len(sequence)-5::]
        
        yy=  smiler_lists (sequence,set2)
        print(yy)
        yy = sequence[yy]
        print ( " the next prbability might be {}".format(yy))
    if x==0:
        program()


def smiler_lists (set1,set2):
    incrment=0
    yy=0
    for i in set1:
        seti = set1[incrment:incrment+5:1]
        if seti == set2: 
            return yy
        incrment+=1
        if incrment == len(sequence):
            break

program()

Advertisement

Answer

program() expects smiler_lists to return the index after the found match. You’re always returning 0, since you never update yy.

def smiler_lists (set1,set2):
    for incrment in range(0, len(set1)-6):
        seti = set1[incrment:incrment+5:1]
        if seti == set2: 
            return incrment + 5

program() also needs to check if smiler_lists found a match. When it doesn’t find a match, it returns None, and it must check for that.

        yy=  smiler_lists (sequence,set2)
        if yy is not None:
            print(yy)
            yy = sequence[yy]
            print ( " the next prbability might be {}".format(yy))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement