I am trying to write a program that counts votes for two candidates. I seem to be missing something regarding the loop. After it asks how many votes to add I want it to loop back and ask "Add votes for which candidate" and then back to "how many votes to add". For some reason, after the first iteration it just keeps looping to "add votes for which candidate" and skips the "how many votes to add" line.
ie).
Add votes for which candidate? John
How many votes to add? 300
Add votes for which candidate? Mary
How many votes to add? 400
def main():
x = str(input("Give the name of the first candidate: "))
y = str(input("Give the name of the second candidate: "))
vote = str(input("Add votes for which candidate?(<Enter> to quit): "))
if vote == 'John':
total1 = 0.0
x1 = int(input('How many votes to add?: '))
while x1 >= 0:
total1 = total1 + x1
vote = str(input("Add votes for which candidate?(<Enter> to quit): "))
if vote == 'Mary':
total2 = 0.0
x2 = int(input('How many votes to add?: '))
while x2 >= 0:
total2 = total2 + x2
vote = str(input("Add votes for which candidate?(<Enter> to quit): "))
else:
print("\nInvalid candidate name.")
exit()
else:
print("\nInvalid candidate name.")
exit()
print("Total votes", 'John', total1)
print("Total votes", 'Mary', total2)
main()
Aucun commentaire:
Enregistrer un commentaire