fixed last option in assignment-1, changed variable names for better understanding, cleaner code. [perfect code]

This commit is contained in:
K 2023-12-07 14:25:56 +05:30
parent efc3d6820b
commit 39e5ac734c
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -1,117 +1,117 @@
# DSL Assignment - 1 '''
Problem Statement: In second year computer engineering class, group A students play cricket, group B students play badminton and group C students play football.
Write a Python program using functions to compute following:
a) List of students who play both cricket and badminton
b) List of students who play either cricket or badminton but not both
c) Number of students who play neither cricket nor badminton
d) Number of students who play cricket and football but not badminton.
(Note- While realising the group, duplicate entries should be avoided, Do not use SET built-in functions)
# List of players Code from Data Structure Lab (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/DSL
'''
cricket_players=[] # BEGINNING OF CODE
badminton_players=[] # List to store players
football_players=[] cricketPlayers = []
badmintonPlayers = []
footballPlayers = []
# Input values # Input cricket
def cricketInput():
## List for cricket players total = int(input("Total number of CRICKET players:\t"))
def cricket_in():
total=int(input("Total number of students playing cricket: \t"))
for i in range(total): for i in range(total):
players=int(input("Enter roll number: \t")) players = int(input(f"Roll number of player {i+1}:\t"))
cricket_players.append(players) cricketPlayers.append(players)
print("Roll numbers of", total, "students playing cricket: \t", cricket_players) print(f"\n----------\nStudents playing cricket:\t {cricketPlayers}\n----------\n")
cricket_in()
## List for badminton players # Input badminton
def badminton_in(): def badmintonInput():
total=int(input("Total number of students playing badminton: \t")) total = int(input("Total number of BADMINTON players:\t"))
for i in range(total): for i in range(total):
players=int(input("Enter roll number: \t")) players = int(input(f"Roll number of player {i+1}:\t"))
badminton_players.append(players) badmintonPlayers.append(players)
print("Roll numbers of", total, "students playing badminton: \t", badminton_players) print(f"\n----------\nStudents playing badminton:\t {badmintonPlayers}\n----------\n")
badminton_in()
## List for football players # Input football
def football_in(): def footballInput():
total=int(input("Total number of students playing football: \t")) total = int(input("Total number of FOOTBALL players:\t"))
for i in range(total): for i in range(total):
players=int(input("Enter roll number: \t")) players = int(input(f"Roll number of player {i+1}:\t"))
football_players.append(players) footballPlayers.append(players)
print("Roll numbers of", total, "students playing football: \t", football_players) print(f"\n----------\nStudents playing football:\t {footballPlayers}\n----------\n")
football_in()
# List of students playing cricket+badminton # Playing both cricket and badminton
def CricketAndBadminton():
listCricketAndBadminton = []
for i in cricketPlayers:
if i in badmintonPlayers:
listCricketAndBadminton.append(i)
print(f"\n----------\nStudents playing cricket and badminton:\t {listCricketAndBadminton}\n----------\n")
cricket_and_badminton=[] # Either cricket or badminton but not both
def CricketOrBadminton():
listCricketOrBadminton = []
for i in cricketPlayers:
if i not in badmintonPlayers:
listCricketOrBadminton.append(i)
for i in badmintonPlayers:
if i not in cricketPlayers:
listCricketOrBadminton.append(i)
print(f"\n----------\nStudents playing cricket or badminton (but not both):\t {listCricketOrBadminton}\n----------\n")
def cricket_and_badminton_fn(): # Neither cricket nor badminton, i.e. football only
for i in cricket_players: def FootballOnly():
if i in badminton_players: listCricketAndBadminton = []
cricket_and_badminton.append(i) listFootballOnly = []
print("Roll numbers of students playing cricket and badminton are: \t", cricket_and_badminton) for i in cricketPlayers:
listCricketAndBadminton.append(i)
for i in badmintonPlayers:
if i not in cricketPlayers:
listCricketAndBadminton.append(i)
for i in footballPlayers:
if i not in listCricketAndBadminton:
listFootballOnly.append(i)
print(f"\n----------\nTotal students playing football only are:\t{len(listFootballOnly)}\nList:\t{listFootballOnly}\n----------\n")
# List of students playing either cricket or badminton but not both # Cricket and football but not badminton
def notBadminton():
listNotBadminton = []
for i in cricketPlayers:
if i in footballPlayers:
if i not in badmintonPlayers:
listNotBadminton.append(i)
print(f"\n----------\nTotal students not playing badminton (but do play cricket and football):\t{len(listNotBadminton)}\nList:\t{listNotBadminton}\n----------\n")
cricket_or_badminton=[] # Main function with options
def main():
print("----- ENTER ROLL NUMBERS IN LIST -----")
cricketInput()
badmintonInput()
footballInput()
print("----- ROLL NUMBERS SAVED -----\n")
while (1):
print("\n----- MAIN MENU -----")
print("1 -> Students playing both cricket and badminton")
print("2 -> Students playing either cricket or badminton, but not both")
print("3 -> Students playing neither cricket nor badminton (i.e. football only)")
print("4 -> Students playing cricket and football, but not badminton")
print("5 -> Exit")
def cricket_or_badminton_fn(): optn = int(input("Choose an option (1-5):\t"))
for i in cricket_players:
if i not in badminton_players:
cricket_or_badminton.append(i)
for i in badminton_players:
if i not in cricket_players:
cricket_or_badminton.append(i)
print("Roll numbers of students playing either cricket or badminton are: \t", cricket_or_badminton)
# Number of students playing neither cricket nor badminton if (optn == 1):
CricketAndBadminton()
football_only=[] elif (optn == 2):
football_only_1=[] CricketOrBadminton()
elif (optn == 3):
def football_only_fn(): FootballOnly()
for i in cricket_players: elif (optn == 4):
football_only.append(i) notBadminton()
for i in badminton_players: elif (optn ==5):
if i not in cricket_players: print("\n\n## END OF CODE\n\n")
football_only.append(i)
for i in football_players:
if i not in football_only:
football_only_1.append(i)
print("Number of students playing neither cricket nor badminton: \t", len(football_only_1), "\n Roll numbers are:\t", football_only_1)
# Number of students playing cricket and football but not badminton
not_badminton=[]
def not_badminton_fn():
for i in badminton_players:
if i not in cricket_players:
not_badminton.append(i)
for i in badminton_players:
if i not in football_players:
not_badminton.append(i)
print("Number of students playing cricket and football but not badminton: \t", len(not_badminton), "\n Roll numbers are:\t", not_badminton)
# Choosing an option
def choose_optn():
while True:
print("Choose an operation to perform:")
print("1. List of students playing cricket and badminton both")
print("2. List of students playing either cricket or badminton")
print("3. List of students playing neither cricket nor badminton")
print("4. List of students playing cricket and football but not badminton")
print("5. Exit")
optn=int(input("Enter option number (1-5): \t"))
if optn==1:
cricket_and_badminton_fn()
if optn==2:
cricket_or_badminton_fn()
if optn==3:
football_only_fn()
if optn==4:
not_badminton_fn()
if optn==5:
print("## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE")
quit() quit()
else: else:
print("\nPlease choose a valid option.\n") print("\n\nPlease choose a valid option (1-5)\n\n")
choose_optn()
choose_optn()
## END OF CODE
main()
# END OF CODE