[SOLVED] For loop is executing twice, but only returning a value once

Issue

This Content is from Stack Overflow. Question asked by Rev

My code is executing mostly how I want it to but when it prints my values from my function it is doubling the amount of times that the function code is ran. I only want 10 interations but it is printing 20. I would just lower the range to 5 but then it throws off my final score in my block of code that displays who won the most rounds. How can I stop the function code from running twice? TIA

import random
import time
answer = input("Play the game?")

winsP1 = 0
winsP2 = 0

def determineWinner(winsP1, winsP2):

    for wins in range(10):

        from random import randint
        player1 = randint(1,10)
        player2 = randint(1,10)
        #time.sleep(1)
        print("Player 1:", player1)
        #time.sleep(1)
        print("Player 2:", player2)
        #time.sleep(1)
        if player1==player2:
            print("This round is a tie!")
            winsP1 += 1
            winsP2 += 1
        elif player1>player2:
            print("Player 1 wins this round!")
            winsP1 += 1
        elif player2>player1:
            print("Player 2 wins this round!")
            winsP2 += 1         

    return winsP1, winsP2

winsP1, winsP2 = determineWinner(winsP1 = winsP1, winsP2 = 
winsP2)


if answer == "y" or answer == "Y" or answer == "yes" or answer ==  "Yes":

    determineWinner(winsP1, winsP2)

    if winsP1>winsP2:
        print()
        print("The score totals are:")
        print("Player one: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("Player 1 wins with a score of", str(winsP1) +  "!")
        print()
    elif winsP2>winsP1:
        print()
        print("The score totals are:")
        print("Player One: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("Player 2 wins with a score of", str(winsP2) +  "!")
        print()
    elif winsP1==winsP2:
        print()
        print("The score totals are:")
        print("Player one: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("It's a tie!")
        print()



Solution

It’s running 20 times since determineWinner is called twice. Once here winsP1, winsP2 = determineWinner(winsP1 = winsP1, winsP2 = winsP2) and the next time within the if block. You could set winsP1 = winsP2 = 0 instead so as to have it start at a neutral state.

Not entirely sure if you’d like to call the function twice; But that’s the main reason for the 20 prints you see.

import time
answer = input("Play the game?")

winsP1 = 0
winsP2 = 0

def determineWinner(winsP1, winsP2):

    for wins in range(10):

        from random import randint
        player1 = randint(1,10)
        player2 = randint(1,10)
        #time.sleep(1)
        print("Player 1:", player1)
        #time.sleep(1)
        print("Player 2:", player2)
        #time.sleep(1)
        if player1==player2:
            print("This round is a tie!")
            winsP1 += 1
            winsP2 += 1
        elif player1>player2:
            print("Player 1 wins this round!")
            winsP1 += 1
        elif player2>player1:
            print("Player 2 wins this round!")
            winsP2 += 1         

    return winsP1, winsP2

winsP1 = winsP2 = 0

if answer == "y" or answer == "Y" or answer == "yes" or answer ==  "Yes":

    winsP1, winsP2 = determineWinner(winsP1, winsP2)

    if winsP1>winsP2:
        print()
        print("The score totals are:")
        print("Player one: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("Player 1 wins with a score of", str(winsP1) +  "!")
        print()
    elif winsP2>winsP1:
        print()
        print("The score totals are:")
        print("Player One: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("Player 2 wins with a score of", str(winsP2) +  "!")
        print()
    elif winsP1==winsP2:
        print()
        print("The score totals are:")
        print("Player one: " + str(winsP1))
        print("Player two: " + str(winsP2))
        print()
        print("It's a tie!")
        print()


This Question was asked in StackOverflow by Rev and Answered by Davis R. It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?