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


Messages In This Thread
Python code isn't looping player turns - by Reta - May-19-2019, 06:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How would I go about repeating/looping this code? azulu 5 2,188 Mar-23-2020, 02:26 AM
Last Post: azulu
  PLayer vs.PLayer game candylicious 1 3,113 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