Reputation: 9
I'm trying to create an application to play the mastermind game but I've run into a problem and I can't quite figure out what's going wrong. Every time I run the check to see whether the guessed integers are correct, it either returns a value of 1 or 0 (never 2, 3 or 4). Any help in resolving this would be greatly appreciated. Note: the lines where it prints (a,b,c,d) and (r,q,h,l) are purely for troubleshooting purposes
import random
dummy = True
a=int(random.random()*10)
b=int(random.random()*10)
c=int(random.random()*10)
d=int(random.random()*10)
print(a,b,c,d)
while dummy == True:
f=0
if a == 0 or b ==0 or c == 0 or d == 0:
#run program again
dummy = False
print("Enter your four guesses, separated by commas.")
guess=input()
t,v,w,y=guess.split(",")
print(t,v,w,y)
r=int(t)
q=int(v)
h=int(w)
l=int(y)
if r==a:
f+=1
elif q==b:
f+=1
elif h==c:
f+=1
elif l==d:
f+=1
print(f)
if f==4:
dummy = False
print("Well done!")
Upvotes: 1
Views: 171
Reputation: 5704
Problem is you have to replace the elif with if since if the if statements i true,it wont run the rest elifs:
import random
dummy = True
a=int(random.random()*10)
b=int(random.random()*10)
c=int(random.random()*10)
d=int(random.random()*10)
print(a,b,c,d)
while dummy == True:
f=0
if a == 0 or b ==0 or c == 0 or d == 0:
#run program again
dummy = False
print("Enter your four guesses, separated by commas.")
guess=input()
t,v,w,y=guess.split(",")
print(t,v,w,y)
r=int(t)
q=int(v)
h=int(w)
l=int(y)
if r==a:
f+=1
if q==b:
f+=1
if h==c:
f+=1
if l==d:
f+=1
print(f)
if f==4:
dummy = False
print("Well done!")
Upvotes: 0
Reputation: 45741
The problem is, you're using elif
. As soon as one one of the conditions is true, it skips the others. Change your elif
s to if
s if you want all the checks to run.
Upvotes: 0
Reputation: 1345
You are using elif
which only runs if the previous condition was false (it is an abreviation for "else if"). You likely want separate if
s
if r==a:
f+=1
if q==b:
f+=1
if h==c:
f+=1
if l==d:
f+=1
Your code stops when it increments and as such cannot get bigger than one.
Upvotes: 1