Rep Rep - 13 days ago 5
Python Question

Compound Looping a code

Ignore! I apologize I realized python uses for x in range() for its while loops



#Point Calc System

from random import randint
team1 = 0
team2 = 0
winloss = 0
check = 0
x = 0
i = 0

#while x < 6: - Didnt work, probably formatting issue -
#x += 1
#Runs best of 3 games
while i < 3:
i = i + 1

#2 = win // 1 = loss
winloss = randint(1,2)
check = check + winloss
print("Check = ",check)

if winloss == 2:
team1 = team1 + 2
print("win ",team1)
else:
team2 = team2 + 2
print("loss ",team2)

#Test to see if a Team won 2/3 battles or won 2 in a row.
if team1 > team2 and check == 5:
team1 = team1 + 2
print("team1 wins",team1)

elif team1 > team2 and check == 6:
print("Team1 wins no add",team1)

elif team2 > team1 and check == 4:
team2 = team2 + 2
print("team2 wins",team2)

elif team2 > team1 and check == 3:
print("Team2 wins no add",team2)

else:
#Should never be seen.
print("error")


I thought adding another while loop over top would have worked but it did not, adding a simple: while x < 6: x+=1... didnt work.

Phu Phu
Answer

Most likely, you had an Indentation Error.

If you run Python in a terminal with spaces in front of your code

>>>   x = 1 
File "<stdin>", line 1
    x =5
    ^
IndentationError: unexpected indent

This one execute with no issue

>>>x = 1 

With that said, the below code executes with no issue after getting rid of the incorrect spaces.

#Point Calc System

from random import randint
team1 = 0
team2 = 0
winloss = 0
check = 0
x = 0
i = 0
while x < 6:
    x += 1
    #Runs best of 3 games
    while i < 3:
        i = i + 1
        #2 = win // 1 = loss
        winloss = randint(1,2)
        check = check + winloss
        print("Check = ",check)

        if winloss == 2:
            team1 = team1 + 2
            print("win ",team1)
        else:
            team2 = team2 + 2
            print("loss ",team2)

    #Test to see if a Team won 2/3 battles or won 2 in a row.
    if team1 > team2 and check == 5:
        team1 = team1 + 2
        print("team1 wins",team1)

    elif team1 > team2 and check == 6:
        print("Team1 wins no add",team1)

    elif team2 > team1 and check == 4:
        team2 = team2 + 2
        print("team2 wins",team2)

    elif team2 > team1 and check == 3:
        print("Team2 wins no add",team2)

    else:
        #Should never be seen.
        print("error")   

Note : Also remember to not mix tab and spaces (in your indentation inside a while loop or if/else statement)