May-19-2019, 06:07 AM
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 :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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() |