pythonif-statementwhile-loop

Python While loop is executed, but then it fails to execute an if statement after it


I am working on a simple 2 player dice rolling game and have found a problem that i haven't been able to fix, after a While loop, it refuses to execute an if statement

def Main():
global player1_score, player2_score
rounds_input = int(input('How many rounds do you want?: '))
i = rounds_input
n = 0
while n < i:
    gameFunc()
    n += 1
else:
    if player1_score > player1_score:
        print("Player 1 Wins Game", count)   
    elif player1_score < player1_score:
        print("Player 2 Wins Game", count) 

The code is meant to take users input and execute gameFunc() that many times, once this has been completed, I want it to check who won the most and print the corresponding Player Wins Game x. Where x is how many times game is read in a text file called leaderboard.txt + 1

def gameFunc():
global player1_score, player2_score
player1_roll = random.randint(0, 20)
player2_roll = random.randint(0, 20)
print("player 1 rolled", player1_roll)
print("player 2 rolled", player2_roll)

if player1_roll > player2_roll:
    print("Player 1 Wins!!" + "\n")
    player1_score += 1
elif player1_roll < player2_roll:
    print("Player 2 Wins!!" + "\n")
    player2_score += 1
else:
    print("It's a Tie!!" + "\n")

The entire code is as follows:

import random

word = "game"
count = 0
game = count + 1
player1_score = 0
player2_score = 0
with open("leaderboard.txt", 'r') as f:
    for line in f:
        words = line.split()
        for i in words:
            if(i==word):
                count=count+1
print("Occurrences of the word", word,"is", count)
print("Game number ", game)

def gameFunc():
    global player1_score, player2_score
    player1_roll = random.randint(0, 20)
    player2_roll = random.randint(0, 20)
    print("player 1 rolled", player1_roll)
    print("player 2 rolled", player2_roll)

    if player1_roll > player2_roll:
        print("Player 1 Wins!!" + "\n")
        player1_score += 1
    elif player1_roll < player2_roll:
        print("Player 2 Wins!!" + "\n")
        player2_score += 1
    else:
        print("It's a Tie!!" + "\n")

def Main():
    global player1_score, player2_score
    rounds_input = int(input('How many rounds do you want?: '))
    i = rounds_input
    n = 0
    while n < i:
        gameFunc()
        n += 1
    else:
        if player1_score > player1_score:
            print("Player 1 Wins Game", count)   
        elif player1_score < player1_score:
            print("Player 2 Wins Game", count) 

def Start():
    print("Do you want to begin?")
    beginPrompt = input('Type START to begin: ').lower()
    if beginPrompt == 'start':
        print("\n")
        Main()
    else:
        print("\n" + "Try Again" + "\n")
        Start()

Start()

Solution

  • In your code in the logic of the final if statement in the Main() function:

    if player1_score > player1_score:
    

    This line always set to False because you're comparing player1_score to itself, not to player2_score. Just like this one:

    elif player1_score < player1_score:
    

    It should be something like this:

    if player1_score > player2_score:
        print("Player 1 Wins Game", count + 1)  # You want something "count + 1" here
    elif player1_score < player2_score:
        print("Player 2 Wins Game", count + 1)
    else:
        print("It's a Draw!")
    

    Also, you're printing Game, but you're using the value count from how many times "game" appeared in the leaderboard file. You set:

    game = count + 1
    

    But then you never use game, only count, which is the number of previous games. So you probably want to show the next game number as count + 1.

    Your def Main(): should look like this:

    def Main():
        global player1_score, player2_score
        rounds_input = int(input('How many rounds do you want?: '))
        i = rounds_input
        n = 0
        while n < i:
            gameFunc()
            n += 1
        else:
            if player1_score > player2_score:
                print("Player 1 Wins Game", count + 1)
            elif player1_score < player2_score:
                print("Player 2 Wins Game", count + 1)
            else:
                print("It's a Draw!")