Voting Program Python
Question:
I am not able to solve my code problem.
The registration part works correctly, the listing part also, the problem is in the voting, I can vote as many times as I want for the candidate who is in position [0] on the list but not for the others.
I’ve tried changing it several times but none of it fixes the problem.
token= []
num = []
result= []
print('#'*40)
print(" Voting CIPA Tabajara Ltda")
print('#'*40)
def menu():
while True:
print('#'*40)
print('n1 - Register candidate')
print('2 - Candidate list')
print('3 - Vote')
print('4 - Result')
print('n')
print('To close, press ENTER')
x=input()
if x=='1':
registration()
if x=='2':
token()
if x=='3':
voting()
if x=='4':
verification()
if x=='':
print("END OF PROGRAM!")
break
def registration():
print('#'*40)
print('CANDIDATE REGISTRATION')
print('#'*40)
while True:
name=str(input('Name: '))
num=int(input('Code: '))
vote=int(0)
token.append([name,num,vote])
resp=str(input("Do you want to continue? If not press ENTER!"))
if resp in '':
break
def list():
print('#'*40)
print('LIST OF CANDIDATES')
print('#'*40,'n')
#print(token)
print(f'{"Name":<16}{"Number":5}')
for i, a in enumerate(token):
print(f'{a[0]:<16}{a[1]:5}')
menu()
def voting():
print('#'*40)
print('VOTING')
print('#'*40)
print('Enter your vote!')
vote=int(input())
while True:
for i in range(0,len(token),1):
if token[i][1] != vote:
print('n')
print("#"*40)
print('CANDIDATE NOT REGISTERED!')
print("#"*40)
print('n')
menu()
if token[i][1] == vote:
print('n')
token[i][2]+=1
print(token)
menu()
if vote=='':
menu()
print('Enter your vote!')
voto=int(input())
def verification():
print('#'*40)
print('RESULTS')
print('#'*40)
print(result)
menu()
Answers:
There are other errors too besides the one you asked of. I will list them after the main one.
Firstly, in your voting method:
def voting():
...
while True:
# you loop through the list. okay.
for i in range(0,len(token),1):
# you approach the first registration and check if it's your target.
# if it's not you're leaving the method! and making the call recursive! this is highly inefficient.
if token[i][1] != vote:
...
menu()
# you should continue to check more cases and not leave the loop with a different method call.
if token[i][1] == vote:
print('n')
token[i][2]+=1
print(token)
# again making a recursive call is absolutely inefficient. you can simply return.
menu()
if vote=='':
menu()
print('Enter your vote!')
# TYPO
voto=int(input())
This is how I would implement the voting method, keeping in mind your method mostly:
def vote(target):
# you could run this in an infinite loop to take multiple votes.
for i in range(len(token)):
if token[i][1] == target:
token[i][2] += 1
print(token)
return
print("candidate not found")
Remember that the job of this method is to vote for somebody. I would not make it accept values, although you totally can and it might not be bad design. This is just how I like to design my methods. So you would take the input in your menu()
method and call the vote function <I would name it vote because by convention, since methods do something, I name them after verbs>.
Again, the vote(int)
method can and should be made differently but this is kind of in-line with your design. I don’t want to impose mine.
Now, talking about the rest of what I encountered:
- I would prefer using a
match
construct. (this is new in Python 3.10)
- You have tried calling your token list as
token()
in the case 2. You probably wanted to write print(token)
def list()
is not the best thing. list() is already a method in python and this declaration might conflict with it, masking it in essence.
- You could consider a different structure to store your candidates.
Also, to show that your recursive call is not a good idea:
To close, press ENTER
^CTraceback (most recent call last):
File "/Users/esskayesss/code/tmp/so.py", line 108, in <module>
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 92, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 92, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 86, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 86, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 22, in menu
x = input()
KeyboardInterrupt
I am not able to solve my code problem.
The registration part works correctly, the listing part also, the problem is in the voting, I can vote as many times as I want for the candidate who is in position [0] on the list but not for the others.
I’ve tried changing it several times but none of it fixes the problem.
token= []
num = []
result= []
print('#'*40)
print(" Voting CIPA Tabajara Ltda")
print('#'*40)
def menu():
while True:
print('#'*40)
print('n1 - Register candidate')
print('2 - Candidate list')
print('3 - Vote')
print('4 - Result')
print('n')
print('To close, press ENTER')
x=input()
if x=='1':
registration()
if x=='2':
token()
if x=='3':
voting()
if x=='4':
verification()
if x=='':
print("END OF PROGRAM!")
break
def registration():
print('#'*40)
print('CANDIDATE REGISTRATION')
print('#'*40)
while True:
name=str(input('Name: '))
num=int(input('Code: '))
vote=int(0)
token.append([name,num,vote])
resp=str(input("Do you want to continue? If not press ENTER!"))
if resp in '':
break
def list():
print('#'*40)
print('LIST OF CANDIDATES')
print('#'*40,'n')
#print(token)
print(f'{"Name":<16}{"Number":5}')
for i, a in enumerate(token):
print(f'{a[0]:<16}{a[1]:5}')
menu()
def voting():
print('#'*40)
print('VOTING')
print('#'*40)
print('Enter your vote!')
vote=int(input())
while True:
for i in range(0,len(token),1):
if token[i][1] != vote:
print('n')
print("#"*40)
print('CANDIDATE NOT REGISTERED!')
print("#"*40)
print('n')
menu()
if token[i][1] == vote:
print('n')
token[i][2]+=1
print(token)
menu()
if vote=='':
menu()
print('Enter your vote!')
voto=int(input())
def verification():
print('#'*40)
print('RESULTS')
print('#'*40)
print(result)
menu()
There are other errors too besides the one you asked of. I will list them after the main one.
Firstly, in your voting method:
def voting():
...
while True:
# you loop through the list. okay.
for i in range(0,len(token),1):
# you approach the first registration and check if it's your target.
# if it's not you're leaving the method! and making the call recursive! this is highly inefficient.
if token[i][1] != vote:
...
menu()
# you should continue to check more cases and not leave the loop with a different method call.
if token[i][1] == vote:
print('n')
token[i][2]+=1
print(token)
# again making a recursive call is absolutely inefficient. you can simply return.
menu()
if vote=='':
menu()
print('Enter your vote!')
# TYPO
voto=int(input())
This is how I would implement the voting method, keeping in mind your method mostly:
def vote(target):
# you could run this in an infinite loop to take multiple votes.
for i in range(len(token)):
if token[i][1] == target:
token[i][2] += 1
print(token)
return
print("candidate not found")
Remember that the job of this method is to vote for somebody. I would not make it accept values, although you totally can and it might not be bad design. This is just how I like to design my methods. So you would take the input in your menu()
method and call the vote function <I would name it vote because by convention, since methods do something, I name them after verbs>.
Again, the vote(int)
method can and should be made differently but this is kind of in-line with your design. I don’t want to impose mine.
Now, talking about the rest of what I encountered:
- I would prefer using a
match
construct. (this is new in Python 3.10) - You have tried calling your token list as
token()
in the case 2. You probably wanted to writeprint(token)
def list()
is not the best thing. list() is already a method in python and this declaration might conflict with it, masking it in essence.- You could consider a different structure to store your candidates.
Also, to show that your recursive call is not a good idea:
To close, press ENTER
^CTraceback (most recent call last):
File "/Users/esskayesss/code/tmp/so.py", line 108, in <module>
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 92, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 92, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 86, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 31, in menu
voting()
File "/Users/esskayesss/code/tmp/so.py", line 86, in voting
menu()
File "/Users/esskayesss/code/tmp/so.py", line 22, in menu
x = input()
KeyboardInterrupt