Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need help with Bulls and Cows program

+2 votes
asked Feb 19, 2022 by Kokoz (340 points)
reopened Feb 23, 2022 by Kokoz
Hello all,
I am relatively new to programming (started learnig 7-8 months ago from youtube and other sites).
I would like to get an overall evaluation of my code and guidelines on how to make checking if the input is valid easier, because now it doesn't work as I want it to work.

Here you can find my code:  https://onlinegdb.com/rSoywgXsB

Thank you for your time and help :)

1 Answer

+1 vote
answered Feb 22, 2022 by Peter Minarik (88,130 points)
selected Feb 22, 2022 by Kokoz
Best answer

What exactly doesn't work the way you'd want it?

I see this in your code (repeated in multiple places):

while not comp_bulls.isnumeric() or len(comp_bulls) != 1 or int(comp_bulls) > 4:
    comp_bulls = input("Invalid input. Please type a number between 1 and 4: ")

It seems an OK check for me. (I'm not a Python developer.) You check if the value of input (, which is a string) is a number indeed, then you cast it to int. I don't think you'd need to check the length is not 1. It would be enough to check if the number is larger than 4 (or perhaps less than your minimum, 0?).

You could move this check into a separate function so you wouldn't need to copy-paste your code.

commented Feb 22, 2022 by Kokoz (340 points)
edited Feb 22, 2022 by Kokoz
Thank you for your answer :)
About the input checker- below the bulls check I have comp_cows check. And after that I check if the sum of both is > 4. But at this point I dont have the previous checks. So the user could input something wich will break the code.
I will try with a check function, as you said, and see if that fixes it. I need to improve my arranging skills, because sometimes I struggle with "if"and "while" statements.
Again, thank you

This is how it looks now - https://onlinegdb.com/nyNk5Cz_9
I made valid_comp and valid_hum functions and I think it is a lot better now. I tested it as far as I could and so far it works every time :}
If you have any other recommendations or critiques, please, feel free to tell me.
Thank you
commented Feb 22, 2022 by Peter Minarik (88,130 points)
Just a minor performance hint: you can save a bit of computation time by not turning your "the_input" into actual numbers multiple times for your check but only once:

def valid_comp(the_input):
    value = int(the_input) if the_input.isnumeric() else -1
    if value > 4 or value < 0:
        return valid_comp(input("Invalid input. Please type a number between 1 and 4: "))
    return int(the_input)

Your function and instruction are contradicting. You say that the expected number must be "between 1 and 4", but your code reads: must not be larger than 4 or smaller than 0, that is it must be between 0 and 4.

You should fix that. :)

In "valid_hum", there's not much point in checking if the number is less than 0 when you'll verify if it is contained in the "checking" list.
commented Feb 22, 2022 by Kokoz (340 points)
I fixed all the things.  I just overlooked the "between 1 and 4" thing.. Thank you for pointing it out.
Now, I am going to translate this into pygame..

Thank you very much for your time and help. I truly appreciate it :)
Wish you all the best
commented Feb 23, 2022 by Peter Minarik (88,130 points)
My pleasure!

Keep on learning programming. It can be a great tool at your disposal.

Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.