GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN

Undercover Auction: A Python Game to Test Your Bidding Skills

Are you ready to create a fun undercover auction game in Python? In this blog, you will learn how to create a bidding game that generates a random winning bid amount, where players will take turns bidding on the amount, without knowing what the winning bid amount is. The player with the lowest bid that is equal to or greater than the winning bid amount will win the auction.

Al-Junaed Islam

--

Image from Istockphoto.com

Step-by-Step Explanation of Code

Before we get started, you need make sure Python is installed on your computer. After that, open your favorite text editor and create a new Python file name undercover_auction.py.

art.py

logo = '''
___________
\ /
)_______(
|"""""""|_.-._,.---------.,_.-._
| | | | | | ''-.
| |_| |_ _| |_..-'
|_______| '-' `'---------'` '-'
)"""""""(
/_________\\
.-------------.
/_______________\\
'''

undercover_auction.py

The code starts by importing the logo from the art module and other required libraries. The art module is used to print ASCII art and the random and os modules are used for generating random numbers and clearing the console screen, respectively.

from art import logo
import random
import os

listing_bidders function takes an input argument, bidders, which is the number of bidders that will participate in the auction. The function uses a forloop to ask each bidder for their name and bid, stores their bids and names in a dictionary, and appends the dictionary to a list. Finally, the function returns the list of bidders with their respective bids.

def listing_bidders(bidders):
bid_list = []
bid_dict = {}
for _ in range(bidders):
clear()
name = input("What is your name? - ")
bid = int(input("What is your bid? - "))
bid_dict = {name: bid}
bid_list.append(bid_dict)

return bid_list

filter_bid function takes a list of bidders with their respective bids and filters out all bids that are less than the winning bid.

def filter_bid(bid_list):
for bidders in bid_list:
for key in bidders:
if bidders.get(key) < winning_bid:
bid_list.remove(bidders)

find_minimum function takes a list of bidders with their respective bids and finds the bidder with the lowest bid that is equal to or greater than the winning bid.

def find_minimum(bid_list):
for key in bid_list[0]:
minimum_bid = bid_list[0][key]
minimum_name = key

for bidders in bid_list:
for key in bidders:
if bidders[key] < minimum_bid:
minimum_bid = bidders[key]
minimum_name = key

return minimum_name, minimum_bid

After defining theclear function using a lambda function that clears the terminal, the code prints an ASCII art logo and the rules of the auction.

clear = lambda: os.system('cls')

print(logo)

print("Welcome To The Undercover Auction. Please Read The Rules Below.\n")

print("1. A random winning bid amount is generated and kept a secret.\n 2. Players will take turns bidding on the winning bid amount, without knowing what the winning bid amount is\n 3. Players will bid in whole numbers only\n 4. The player with the LOWEST BID that is equal to or greater than the winning bid amount will win the auction.\n 5. If there are no bids that are equal to or greater than the winning bid amount, the auction will be canceled.\n 6. The game ends when a player wins the auction.\n 7. The winner will be announced at the end of the game, along with the winning bid amount.\n")

The code generates a random winning bid between 0 and 100 using the random module.

winning_bid = round(random.random() * 100)

The code prompts the user to input the number of bidders that will participate in the auction. The listing_bidders function is called with the number of bidders as the input argument, and the returned list is stored in list_of_bidders.

number_of_bidders = int(input("How Many Bidders Will Join? - "))

list_of_bidders = listing_bidders(number_of_bidders)

The filter_bid function is called with list_of_bidders as the input argument to filter out all bids that are less than the winning bid.

# Filtering bids which is less than the winning bid
filter_bid(list_of_bidders)

The find_minimum function is called with list_of_bidders as the input argument to find the bidder with the lowest bid that is equal to or greater than the winning bid. The code prints the name of the winner along with their winning bid.

# Find the minimum bidder
minimum_name, minimum_bid = find_minimum(list_of_bidders)

print("Congratulation!!")
print(f"{minimum_name} won with the minimum bid and the bid was {minimum_bid}")

Here is the full code altogether.

Sample Run

Test Run of The Code

Features

Program’s Feature

  • Listing of bidders with their name and bid amount.
  • Filtering of bids less than the winning bid amount.
  • Finding the minimum bid that meets or exceeds the winning bid amount.
  • Use of lambda function to clear the console.
  • Importing the logo from the art module to make the game more fun.

Python Concepts and Techniques Used

  • Importing modules: The code imports the art module to use the logo in the game.
  • Functions: The code utilizes functions to break down the problem into smaller parts to make it more manageable.
  • Loops: The code uses for loops to iterate over the list of bidders to get their bids.
  • Conditional statements: The code usesifconditional statements to filter bids less than the winning bid amount.
  • Lambda function: The code uses a lambda function to clear the console.

Finally, you learned how to use functions, loops, and conditional statements to develop an undercover auction game in Python. You’ve also learnt how to use modules, such as the art module, to make your game more visually interesting. It’s now your time to develop your own unique game based on these concepts.

This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!

--

--