Python Forum
Python code isn't looping player turns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code isn't looping player turns
#1
Hey all, so I was tasked to create a Double Hog game. For the most part, with help from my teacher, my code works 95%. However, as for this game, the game loops until a player in the lobby reaches 100 points, in which point a congratulations message appears. But in my code, as soon as each player finishes a turn, e.g. all 4 players get a turn, instead of looping, the code just ends. Also, I would like help for the congratulations message, when printed, how do I make it in a way that it prints out the first user that reaches 100 points? Any help would be much appreciated :)

import random

def rules():
    print("-------------------------RULES-------------------------")
    print("1. No cheating \n2. No sabotaging \n3. Have fun, but not too much")
    print("-------------------------RULES-------------------------")


def instructions():
    print ("-------------------------MAIN MENU-------------------------")
    print ("1. Play a game \n2. See the Rules \n3. Exit")
    print ("-------------------------MAIN MENU-------------------------")
    while True:     
        userAsk=int(input("Enter number of choice"))
        if userAsk == 1:
            break
        elif userAsk == 2:
            print(rules())
            userAgain = input("Let's game?")
            if userAgain == "Yes":
                break
        elif userAsk == 3:
            print("Exiting")
            raise SystemExit


def num_players():
    while True:
        players = input("How many players will be playing? ")

        if players.isdigit():
            return int(players)
        else:
            print ("\nPlease enter a valid number of players.\n")


def name_players(players):
    count = 1
    list_of_players = []
    for i in range(players):
        name = input("Enter the name for Player {}: ".format(count))
        list_of_players.append(name)
        count += 1
    print ("")
    return list_of_players


def start_game(list_of_players):
    points = 0 
    for player in list_of_players:
        print("{0} has {1} points.".format(player, points))
    print ("==================================================\n")
    s = int(input("How many sides of the dice do you want? "))
    for player in list_of_players:
        print ("\n{0}'s turn:".format(player))
        answer = input("Press y to roll the dice?")
        while answer == 'y' and points <= 100:
            roll = random.randrange(1, s + 1, 1)  
            if roll > 1:
                points += roll
                print("{0} has {1} points.".format(player, points))
                answer = input("Press y to roll the dice?")

def main():
    instructions()
    players = num_players()
    list_of_players = name_players(players)
    start_game(list_of_players)


if __name__ == "__main__":
    main()
Reply
#2
In this game, you type in the number of players and the player names. Then the player plays their turn, are they supposed to role just once, then the next person goes. First person to 100 wins. If that is the case I would take out the while loop in start_game. Put the "for loop" in a "while loop". The while loop will continue until one player has reached 100 or more pts. When the while loop ends, it will check who has the over 100 pts and then print that they Won.
Reply
#3
(May-19-2019, 06:18 AM)SheeppOSU Wrote: In this game, you type in the number of players and the player names. Then the player plays their turn, are they supposed to role just once, then the next person goes. First person to 100 wins. If that is the case I would take out the while loop in start_game. Put the "for loop" in a "while loop". The while loop will continue until one player has reached 100 or more pts. When the while loop ends, it will check who has the over 100 pts and then print that they Won.

The player loops as much as they want until they roll either a 1 in conjunction with any other number, e.g. they first roll a 1 then rolls a 5 next, this will cause them the ability to roll again and code will move on to next player. I didn't include it in this sample of code, sorry
Reply
#4
Alright, well right below the points = 0, put a while loop how I described above. Make sure to add a print at the very end outside the while loop and when the while loop ends, check for which player has won, and then print the winner
checking the winner -
Have a variable to store current player playing. When the game ends, that variable will be holding the string value of the player's name who won
Reply
#5
This is the homework way of doing it:

player_index = -1
while True:
    player_index = (player_index + 1) % len(list_of_players)
    player = list_of_players[player_index]
    # code for the player's turn goes here.
    if points >= 100:
        break
The modulus operator (%) brings the player_index back to 0 when it goes past the end of the list. However, there is an easier way to do this with the itertool module:

for player in itertools.chain(list_of_players):
    # code for the player's turn goes here.
    if points >= 100:
        break
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(May-19-2019, 06:29 AM)SheeppOSU Wrote: Alright, well right below the points = 0, put a while loop how I described above. Make sure to add a print at the very end outside the while loop and when the while loop ends, check for which player has won, and then print the winner
checking the winner -
Have a variable to store current player playing. When the game ends, that variable will be holding the string value of the player's name who won
So I put the while loop right below the line where I declared points, but now, no matter the rolls each player gets, they all get the same points? Here's what it looks like when outputted:

How many players will be playing? 2
Enter the name for Player 1: Josh
Enter the name for Player 2: John

Josh has 0 points.
John has 0 points.
==================================================

How many sides of the dice do you want? 6

Josh's turn:
Press y to roll the dice?y
Josh has 4 points.
Press y to roll the dice?y
Josh has 6 points.
Press y to roll the dice?y
Josh has 9 points.
Press y to roll the dice?n

John's turn:
Press y to roll the dice?y
John has 14 points.
Press y to roll the dice?n
Josh has 14 points.
John has 14 points.
==================================================
I thought that the points variable is independent to each different player? What am I missing?
Reply
#7
You need to reset points to 0 every time through the loop.

Are you intending that when you stop rolling you get to keep the points you rolled this turn, and then add to them next turn? In that case you are going to need a list, or better yet a dictionary, to keep a separate point total for each player. Then if they stop before getting a one, you add points to their stored point total.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
Sorry, it was late at night when I helped you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How would I go about repeating/looping this code? azulu 5 2,101 Mar-23-2020, 02:26 AM
Last Post: azulu
  PLayer vs.PLayer game candylicious 1 3,037 Nov-04-2017, 08:33 PM
Last Post: Lux

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020