'Validation on lists in python
I am trying to add a feature to my code that will check to see if a tournament already exists. For a tournament to exist, basically means that there will be teams inputted into the program by my user.
I am trying to add the code at the point where my program asks the user for an option to create, edit, view leaderboard or exit program. if option 2 is selected but there are no teams, then an error should be shown to the user to inform them of this please
what code would i need to add for this to work please?
import time
import sys
import os
class Team:
def __init__(self, num, name, size, score):
self.num = num
self.name = name
self.size = size
self.score = score
def add_victory(self):
self.score += 1
def __repr__(self):
return f'Team Number: {self.num} |-| Team Name: {self.name} |-| Member Count: {self.size} |-| Team Score: {self.score}'
def NewTournament():
TeamCounter=int(input('How many Teams will be in the tournament? '))
print('')
for i in range(TeamCounter):
NameOfTeam=input(f'Please Enter Team {i+1} Name: ')
MemberCount=int(input('How Many Members in Team? '))
print('')
teams.append( Team( i+1, NameOfTeam, MemberCount, 0) )
def Score(teams):
EventName=input('Which Event Was Completed? ')
winner=input('Which Team Won the Event? ')
print('Event Winner:', winner, '||', 'Event:',EventName)
print('Is this correct? Y/N')#confirming choice, to avoid any mistakes that the user may have made
Check=input('')
if Check=='y':
print('Updating Leaderboard')
for team in teams:
if team.name == winner:
team.add_victory()
break
print('Updated Leaderboard')
if Check=='n':
print('error')
print('Leaderboard Not Updated')
Menu()
def Leaderboard():
for t in teams:
print(t)
def Menu():
MenuLoop=1
while MenuLoop==1:
print('1.Create Tournament')
print('2.Update Existing Tournament')
print('3.View Leaderboard')
print('4.Exit')
MenuOption=input('')
if MenuOption=='1':
print('Creating Tournament')
NewTournament()#runs the new tournament function
MenuLoop-=1
Menu()
elif MenuOption=='2':
print('Updating Tournament')
MenuLoop-=1
Score(teams)
Menu()
elif MenuOption=='3':
MenuLoop-=1
Leaderboard()
print('')
time.sleep(0.3)
Menu()
elif MenuOption=='4':
print('Exiting Program...')
else:
print('Error, please choose an option from the list below.')#keeps looping if user is not choosing a correct number from list
#start of program
teams = []
print('░██╗░░░░░░░██╗███████╗██╗░░░░░░█████╗░░█████╗░███╗░░░███╗███████╗')
print('░██║░░██╗░░██║██╔════╝██║░░░░░██╔══██╗██╔══██╗████╗░████║██╔════╝')
print('░╚██╗████╗██╔╝█████╗░░██║░░░░░██║░░╚═╝██║░░██║██╔████╔██║█████╗░░')
print('░░████╔═████║░██╔══╝░░██║░░░░░██║░░██╗██║░░██║██║╚██╔╝██║██╔══╝░░')
print('░░╚██╔╝░╚██╔╝░███████╗███████╗╚█████╔╝╚█████╔╝██║░╚═╝░██║███████╗')
print('░░░╚═╝░░░╚═╝░░╚══════╝╚══════╝░╚════╝░░╚════╝░╚═╝░░░░░╚═╝╚══════╝')#welcome user
print('')
Username=input('Enter Username: ')
Password=input('Enter Password: ')
if Username =='Admin' and Password == 'Admin':#very basic login system for security of school
message='Logging In...'
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.10)
print('')
print('User Verified')
time.sleep(0.5)
Menu()
else:
print('User Does Not Exist.')#stops pupils gaining unauthorised access
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
