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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out