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():
total = int(input("Total number of CRICKET players:\t"))
for i in range(total):
players = int(input(f"Roll number of player {i+1}:\t"))
cricketPlayers.append(players)
print(f"\n----------\nStudents playing cricket:\t {cricketPlayers}\n----------\n")
## List for cricket players # Input badminton
def cricket_in(): def badmintonInput():
total=int(input("Total number of students playing cricket: \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"))
cricket_players.append(players) badmintonPlayers.append(players)
print("Roll numbers of", total, "students playing cricket: \t", cricket_players) print(f"\n----------\nStudents playing badminton:\t {badmintonPlayers}\n----------\n")
cricket_in()
## List for badminton players # Input football
def badminton_in(): def footballInput():
total=int(input("Total number of students playing badminton: \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"))
badminton_players.append(players) footballPlayers.append(players)
print("Roll numbers of", total, "students playing badminton: \t", badminton_players) print(f"\n----------\nStudents playing football:\t {footballPlayers}\n----------\n")
badminton_in()
## List for football players # Playing both cricket and badminton
def football_in(): def CricketAndBadminton():
total=int(input("Total number of students playing football: \t")) listCricketAndBadminton = []
for i in range(total): for i in cricketPlayers:
players=int(input("Enter roll number: \t")) if i in badmintonPlayers:
football_players.append(players) listCricketAndBadminton.append(i)
print("Roll numbers of", total, "students playing football: \t", football_players) print(f"\n----------\nStudents playing cricket and badminton:\t {listCricketAndBadminton}\n----------\n")
football_in()
# List of students playing cricket+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")
cricket_and_badminton=[] # Neither cricket nor badminton, i.e. football only
def FootballOnly():
listCricketAndBadminton = []
listFootballOnly = []
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")
def cricket_and_badminton_fn(): # Cricket and football but not badminton
for i in cricket_players: def notBadminton():
if i in badminton_players: listNotBadminton = []
cricket_and_badminton.append(i) for i in cricketPlayers:
print("Roll numbers of students playing cricket and badminton are: \t", cricket_and_badminton) 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")
# List of students playing either cricket or badminton but not both # 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")
cricket_or_badminton=[] optn = int(input("Choose an option (1-5):\t"))
def cricket_or_badminton_fn(): if (optn == 1):
for i in cricket_players: CricketAndBadminton()
if i not in badminton_players: elif (optn == 2):
cricket_or_badminton.append(i) CricketOrBadminton()
for i in badminton_players: elif (optn == 3):
if i not in cricket_players: FootballOnly()
cricket_or_badminton.append(i) elif (optn == 4):
print("Roll numbers of students playing either cricket or badminton are: \t", cricket_or_badminton) notBadminton()
elif (optn ==5):
# Number of students playing neither cricket nor badminton print("\n\n## END OF CODE\n\n")
football_only=[]
football_only_1=[]
def football_only_fn():
for i in cricket_players:
football_only.append(i)
for i in badminton_players:
if i not in cricket_players:
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