1. (6 pts) Suppose you are counting ballots in an election for Supreme (Yet Some
ID: 3735538 • Letter: 1
Question
1. (6 pts) Suppose you are counting ballots in an election for Supreme (Yet Somehow Democratically Elected) Ruler of the Universe. Assume you have 1,000 candidates in all, each of whom is assigned a unique ID number from 0 to 999. Within a new class named BallotCounter, write a program that processes a set of ballots. The program should run by allowing the user to enter the ID number of the selected candidate on each ballot. The user should be able to do this for as many ballots as needed, until entering a negative value to exit. Upon exiting, display a list of the candidates' ID numbers and their number of votes received, but only if the candidate received at least one vote. Include error checking to ensure that the user can't type in an ID number above 999. Below is an example of what your program might look like while running (underlined parts indicate user input). Enter candidate's ID number (0-999, any negative number to exit): 123 Enter candidate's ID number (0-999, any negative number to exit): Enter candidato's ID number (0-999, any negative nunber to exit) 123 Enter candidate's ID number (0-999, any negative number to exit): Enter candidate's ID number (0-999, any negative number to exit): 662 Enter candidate's ID number (0-999, any negative number to exit): 2342 Invalid ID number Enter candidate's ID number (0-999, any negative number to exit): 121 Enter candidate's ID number (0-999, any negative number to exit): 5 Enter candidate's ID number (0-999, any negative number to exit) 12 Enter candidate's ID number (0-999, any negative number to exit): 1 Enter candidate's ID number (0-999, any negative number to axit): 1000 Invalid ID number' Enter eandidate 's ID number (0-999, any negative number to exit) 12 Enter candidate's ID number (0-999, any negative number to exit) Election Results: Candidate 0: 1 vote (s) Candidate 5: 2 vote(s) Candidate 7: 1 vote (s) Candidate 12: 1 vote (s) Candidate 121: 1 vote (s) Candidate 123: 3 vote (s) Candidate 662: 1 vote (s)Explanation / Answer
Hi,
I have implemented your both request with Python, Please let me know if you have any queries.
1. Code:
file_t = []
#Function to insert into list
def append1(number):
file_t.append(number)
# result function to print the values
def result():
count = 0
file_temp = list(set(file_t))
for i in file_temp:
for j in file_t:
if i == j:
count += 1
print("Candidate {0} : {1} vote(s): ".format(i,count))
count = 0
#main function
def main():
r=range(0,999)
while (1):
number = int(input("Enter Candidate's ID number (0-999, any negative number to exit) : "))
if number in r:
#calling function to append values
append1(number)
else:
print("Invalid ID number! ")
#calling to print the result
result()
exit(0)
if __name__== "__main__":
main()
Sample execution:
Enter Candidate's ID number (0-999, any negative number to exit) : 10
Enter Candidate's ID number (0-999, any negative number to exit) : 11
Enter Candidate's ID number (0-999, any negative number to exit) : 10
Enter Candidate's ID number (0-999, any negative number to exit) : 123
Enter Candidate's ID number (0-999, any negative number to exit) : 134
Enter Candidate's ID number (0-999, any negative number to exit) : 156
Enter Candidate's ID number (0-999, any negative number to exit) : 11
Enter Candidate's ID number (0-999, any negative number to exit) : 123
Enter Candidate's ID number (0-999, any negative number to exit) : 156
Enter Candidate's ID number (0-999, any negative number to exit) : 156
Enter Candidate's ID number (0-999, any negative number to exit) : 78
Enter Candidate's ID number (0-999, any negative number to exit) : -1
Invalid ID number!
Candidate 134 : 1 vote(s):
Candidate 10 : 2 vote(s):
Candidate 11 : 2 vote(s):
Candidate 78 : 1 vote(s):
Candidate 123 : 2 vote(s):
Candidate 156 : 3 vote(s):
2. Code:
#intersect function
def intersect(a,b):
print("Intersect values from both list: ",list(set(a) & set(b)))
#Main function
def main():
a=[1,2,3,4,5,6,7,8,9,10]
b=[1,3,5,7,9,11,12,13,14,15]
#Calling intersect function
intersect(a,b)
name=int(input("Enter the number to check.. is it common or not ? : "))
if name in list(set(a) & set(b)):
print("Entered number is common in both friends list")
else:
print("Entered number is not a mutual friend")
if __name__== "__main__":
main()
Sample Execution:
Intersect values from both list: [1, 3, 5, 7, 9]
Enter the number to check.. is it common or not ? : 5
Entered number is common in both friends list
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.