+ 1
Why does this program doesen't work?
vote = input("vote for A,B,C,D") A =0 B =0 C =0 D =0 while vote == "zzzz": if "A": A = A + 1 elif "B": B = B + 1 elif "C": C = C + 1 elif "D": D = D + 1 vote = input("vote for A,B,C,D") print("Results", A,B,C,D)
2 Answers
+ 1
1. That's because You are running the loop only if input is equals to zzzz, but as far as I understood it is supposed to be A B C or D. So In this case your code won't even function as expected.
2. You are using user input twice which is unnecessary in this script, you can use only 1 instead of 2.
I have fixed it for you (make sure you understand what i have done, read the comments that i put in the code below.):
#!/usr/bin/python
## Options available for user input
vote_options = ['A','B','C','D','a','b','c','d']
## Declaring variables
A = 0
B = 0
C = 0
D = 0
## while loop will run until this is set to True by the code below.
over = False
## Run the loop while "over" is set to False - not over means False or 'not True'
while(not over):
## get user input
vote = raw_input("vote for A, B, C, D: ")
## check if its a valid input.
if(vote in vote_options):
if vote == "A" or vote == 'a':
A += 1
elif vote == "B" or vote == 'b':
B += 1
elif vote == "C" or vote == 'c':
C += 1
elif vote == "D" or vote == 'd':
D += 1
print("Results: ", A, B, C, D)
else:
## if the input did not exist in vote_options list `over` will be set to True and while loop will stop.
over = True
0
Cool i tried it works perfect! But how about if i want to leave the option to the user to vote as maby s they want untill they stop it by writing over!!
Shoud i just make a function abd call it? Because i tried it but it doesn't add up the voting