Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

LIST- SCENARIO BASED SAMPLE PYTHON PROGRAMS

1.Shopping List:
Create a program that allows users to add items to a shopping list, remove items, and
display the current list.

def add_item(shopping_list, item):


shopping_list.append(item)
print(f"{item} added to the shopping list.")

def remove_item(shopping_list, item):


if item in shopping_list:
shopping_list.remove(item)
print(f"{item} removed from the shopping list.")
else:
print(f"{item} is not in the shopping list.")

def display_list(shopping_list):
print("Shopping List:")
for item in shopping_list:
print(item)

def main():
shopping_list = []
while True:
print("\n1. Add Item")
print("2. Remove Item")
print("3. Display List")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
item = input("Enter item to add: ")
add_item(shopping_list, item)
elif choice == "2":
item = input("Enter item to remove: ")
remove_item(shopping_list, item)
elif choice == "3":
display_list(shopping_list)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

______________________________________________________________________________
2. To-Do List:
Develop a program that allows users to manage their to-do list by adding tasks, marking
tasks as completed, and displaying the list.

def add_task(todo_list, task):


todo_list.append({"task": task, "completed": False})
print(f"Task '{task}' added to the to-do list.")

def complete_task(todo_list, task):


for item in todo_list:
if item["task"] == task:
item["completed"] = True
print(f"Task '{task}' marked as completed.")
return
print(f"Task '{task}' not found in the to-do list.")

def display_list(todo_list):
print("To-Do List:")
for item in todo_list:
status = "Completed" if item["completed"] else "Not Completed"
print(f"{item['task']} - {status}")

def main():
todo_list = []
while True:
print("\n1. Add Task")
print("2. Complete Task")
print("3. Display List")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
task = input("Enter task to add: ")
add_task(todo_list, task)
elif choice == "2":
task = input("Enter task to mark as completed: ")
complete_task(todo_list, task)
elif choice == "3":
display_list(todo_list)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

______________________________________________________________________________
3. Scoreboard Management:
Write a program that keeps track of scores for a sports event. Allow users to add scores,
remove scores, and display the current scoreboard.

def add_score(scoreboard, team, score):


if team in scoreboard:
scoreboard[team] += score
else:
scoreboard[team] = score
print(f"{score} point(s) added for team {team}.")

def remove_score(scoreboard, team, score):


if team in scoreboard:
scoreboard[team] = max(0, scoreboard[team] - score)
print(f"{score} point(s) removed for team {team}.")
else:
print(f"Team {team} not found in the scoreboard.")

def display_scoreboard(scoreboard):
print("Scoreboard:")
for team, score in scoreboard.items():
print(f"{team}: {score}")

def main():
scoreboard = {}
while True:
print("\n1. Add Score")
print("2. Remove Score")
print("3. Display Scoreboard")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
team = input("Enter team name: ")
score = int(input("Enter score to add: "))
add_score(scoreboard, team, score)
elif choice == "2":
team = input("Enter team name: ")
score = int(input("Enter score to remove: "))
remove_score(scoreboard, team, score)
elif choice == "3":
display_scoreboard(scoreboard)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

______________________________________________________________________________

You might also like