Cows And Bulls Python Code compare() Function Logic
up vote
0
down vote
favorite
import random
import os
import sys
sys.setrecursionlimit(10000)
# global variable for attempts
attempts = 0
# creates a 4-digit random number
def make_random_number():
rand_num = random.randint(1000,9999)
rand_num_list = [int(x) for x in str(rand_num)]
# make sure there are no repeats in number
while len(rand_num_list) > len(set(rand_num_list)):
make_random_number()
return rand_num_list
def make_user_num():
user_num = input("Enter a 4-digit number: ")
user_num_list = [int(x) for x in user_num]
return user_num_list
# compares both numbers and looks for matches, and if in the same position
def compare(rand):
global attempts
# adds to total attempts, if guessed correctly on 1st try, total attempts = 1
attempts += 1
cows = 0
bulls = 0
user = make_user_num()
if rand == user:
print("It's a full match!", rand, "is equal to", user)
print("It took this many attempts:", attempts)
exit()
while rand != user:
print(rand)
# calculates cows
for x in range(4):
if rand[x] == user[x]:
cows += 1
# calculates bulls
for i in range(4):
for j in range(4):
if user[i] == rand[j]:
bulls += 1
I think the for loop calculating the amount of cows(numbers match and also have the same position) is correct, but the one calculating the bulls(numbers match but have different positions), is incorrect. Is my logic correct for calculating the amount of bulls?
python-3.x
add a comment |
up vote
0
down vote
favorite
import random
import os
import sys
sys.setrecursionlimit(10000)
# global variable for attempts
attempts = 0
# creates a 4-digit random number
def make_random_number():
rand_num = random.randint(1000,9999)
rand_num_list = [int(x) for x in str(rand_num)]
# make sure there are no repeats in number
while len(rand_num_list) > len(set(rand_num_list)):
make_random_number()
return rand_num_list
def make_user_num():
user_num = input("Enter a 4-digit number: ")
user_num_list = [int(x) for x in user_num]
return user_num_list
# compares both numbers and looks for matches, and if in the same position
def compare(rand):
global attempts
# adds to total attempts, if guessed correctly on 1st try, total attempts = 1
attempts += 1
cows = 0
bulls = 0
user = make_user_num()
if rand == user:
print("It's a full match!", rand, "is equal to", user)
print("It took this many attempts:", attempts)
exit()
while rand != user:
print(rand)
# calculates cows
for x in range(4):
if rand[x] == user[x]:
cows += 1
# calculates bulls
for i in range(4):
for j in range(4):
if user[i] == rand[j]:
bulls += 1
I think the for loop calculating the amount of cows(numbers match and also have the same position) is correct, but the one calculating the bulls(numbers match but have different positions), is incorrect. Is my logic correct for calculating the amount of bulls?
python-3.x
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
import random
import os
import sys
sys.setrecursionlimit(10000)
# global variable for attempts
attempts = 0
# creates a 4-digit random number
def make_random_number():
rand_num = random.randint(1000,9999)
rand_num_list = [int(x) for x in str(rand_num)]
# make sure there are no repeats in number
while len(rand_num_list) > len(set(rand_num_list)):
make_random_number()
return rand_num_list
def make_user_num():
user_num = input("Enter a 4-digit number: ")
user_num_list = [int(x) for x in user_num]
return user_num_list
# compares both numbers and looks for matches, and if in the same position
def compare(rand):
global attempts
# adds to total attempts, if guessed correctly on 1st try, total attempts = 1
attempts += 1
cows = 0
bulls = 0
user = make_user_num()
if rand == user:
print("It's a full match!", rand, "is equal to", user)
print("It took this many attempts:", attempts)
exit()
while rand != user:
print(rand)
# calculates cows
for x in range(4):
if rand[x] == user[x]:
cows += 1
# calculates bulls
for i in range(4):
for j in range(4):
if user[i] == rand[j]:
bulls += 1
I think the for loop calculating the amount of cows(numbers match and also have the same position) is correct, but the one calculating the bulls(numbers match but have different positions), is incorrect. Is my logic correct for calculating the amount of bulls?
python-3.x
import random
import os
import sys
sys.setrecursionlimit(10000)
# global variable for attempts
attempts = 0
# creates a 4-digit random number
def make_random_number():
rand_num = random.randint(1000,9999)
rand_num_list = [int(x) for x in str(rand_num)]
# make sure there are no repeats in number
while len(rand_num_list) > len(set(rand_num_list)):
make_random_number()
return rand_num_list
def make_user_num():
user_num = input("Enter a 4-digit number: ")
user_num_list = [int(x) for x in user_num]
return user_num_list
# compares both numbers and looks for matches, and if in the same position
def compare(rand):
global attempts
# adds to total attempts, if guessed correctly on 1st try, total attempts = 1
attempts += 1
cows = 0
bulls = 0
user = make_user_num()
if rand == user:
print("It's a full match!", rand, "is equal to", user)
print("It took this many attempts:", attempts)
exit()
while rand != user:
print(rand)
# calculates cows
for x in range(4):
if rand[x] == user[x]:
cows += 1
# calculates bulls
for i in range(4):
for j in range(4):
if user[i] == rand[j]:
bulls += 1
I think the for loop calculating the amount of cows(numbers match and also have the same position) is correct, but the one calculating the bulls(numbers match but have different positions), is incorrect. Is my logic correct for calculating the amount of bulls?
python-3.x
python-3.x
asked Nov 17 at 5:36
DoABarrelRoll94
134
134
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00
add a comment |
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348558%2fcows-and-bulls-python-code-compare-function-logic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have a description of what you are trying to do? I am not familiar with Cows and Bulls.
– FChris
Nov 17 at 14:00