“Whose Gonna Buy For Us” — A Basic Phyton Syntax Game

This game was made as a part of portofolio building for data science bootcamp topic 1 ‘basic phyton’ at Purwadhika.
Picture this: You and your friends are lounging around, craving some comfort items, but none of you are in the mood to venture out. The dilemma? Someone has to go buy those cozy essentials, but you’re all too lazy to decide who. Instead of resorting to boring methods like drawing straws or coercing a friend into running errands, why not inject some excitement? “Whose Gonna Buy For Us” revolutionizes decision-making by infusing it with suspense and strategy.
In this thrilling game, you and your friends can turn a mundane task into an exhilarating experience. With each spin of the chamber, tension builds as you eagerly anticipate who will be the lucky (or unlucky) one to head out on the shopping mission. It’s a unique blend of chance and strategy that adds an element of fun to your lazy hangout.
So why settle for dull decision-making when you can spice things up with “Whose Gonna Buy For Us”?
Code Explanation
The “Whose Gonna Buy For Us” game is a Python program designed to simulate a Russian roulette-style decision-making game. Let’s break down the key components of the code to understand how it works:
Input Validation:
The program starts by prompting the user to input the number of players. It then uses a try-except block to ensure that the input is a valid integer greater than 1. This prevents errors and ensures the game can proceed with the correct number of participants.
def buy_for_us():
while True:
try:
num_players = int(input("Enter the number of players: "))
if num_players <= 1:
raise ValueError("Number of players must be greater than 1")
break
except ValueError:
print("Please enter a valid number of players (greater than 1).")
Player Input:
Next, the program prompts each player to input their name and an item they need to buy. Another try-except block is used here to handle potential errors, such as blank inputs. This ensures that the game proceeds smoothly without any missing information.
players = set()
things_to_buy = []
for i in range(1, num_players + 1):
while True:
try:
player_name = input(f"Enter name for Player {i}: ")
if not player_name.strip():
raise ValueError("Player name cannot be blank.")
break
except ValueError as e:
print("Error:", e)
players.add(player_name)
while True:
try:
thing = input(f"Enter something that {player_name} needs: ")
if not thing.strip():
raise ValueError("Item cannot be blank.")
break
except ValueError as e:
print("Error:", e)
things_to_buy.append(thing)
Game Execution:
Once all players have entered their names and items, the game begins. It simulates the Russian roulette game by taking turns “spinning the chamber” and “pulling the trigger.” The last player standing is considered the “loser” of the game.
print("\nWelcome to 'Whose Gonna Buy For Us'!\n")
print("The game will start with", len(players), "players.")
while len(alive_players) > 1:
print("\nRemaining Players:", ', '.join(alive_players))
input("Press Enter to spin the chamber and pull the trigger...")
print("DAR DER DOR!")
input("\nPress Enter to see who's out of the game...")
dead_player = alive_players.pop()
print(dead_player, "congrats! you are out of the game!")
Output:
After the game concludes, the program displays the list of items that need to be bought. It then announces the final player who lost the game and is responsible for purchasing the items.
print("\nThe game is over!")
print("\nThe following items that need to be bought:")
for item in things_to_buy:
print("-", item)
print("\nThe last player remaining,", alive_players.pop(), ", has lost and will buy things for all the players.")
Overall, this code demonstrates the use of input validation, error handling, and basic game logic in Python. It provides a fun and interactive way to understand these concepts while also showcasing how programming can be used to create entertaining games.
Link to code:
If you’d like to see all the syntax, feel free to check my github at https://github.com/ridhoaryawann/Whose_Gonna_Buy_For_Us