2

I have previously studied Visual Basic for Applications and am slowly getting up to speed with python this week. As I am a new programmer, please bear with me. I understand most of the concepts so far that I've encountered but currently am at a brick wall.

I've written a few functions to help me code a number guessing game. The user enters a 4 digit number. If it matches the programs generated one (I've coded this already) a Y is appended to the output list. If not, an N.

EG. I enter 4567, number is 4568. Output printed from the list is YYYN.

import random 

def A(): 
    digit = random.randint(0, 9)
    return digit 

def B(): 
    numList = list() 
    for counter in range(0,4):
        numList.append(A()) 
    return numList 


def X():
    output = []

    number = input("Please enter the first 4 digit number: ")

    number2= B()



for i in range(0, len(number)):
    if number[i] == number2[i]:
        results.append("Y")
    else:
        results.append("N") 

print(output)

X()

I've coded all this however theres a few things it lacks:

  1. A loop. I don't know how I can loop it so I can get it to ask again. I only want the person to be able to guess 5 times. I'm imagining some sort of for loop with a counter like "From counter 1-5, when I reach 5 I end" but uncertain how to program this.

  2. I've coded a standalone validation code snippet but don't know how I could integrate this in the loop, so for instance if someone entered 444a it should say that this is not a valid entry and let them try again. I made an attempt at this below.

    while myNumber.isnumeric() == True and len(myNumber) == 4:
        for i in range(0, 4)):  
            if myNumber[i] == progsNumber[i]:
                outputList.append("Y")
            else:
                outputList.append("N")
    

Made some good attempts at trying to work this out but struggling to patch it all together. Is anyone able to show me some direction into getting this all together to form a working program? I hope these core elements that I've coded might help you help me!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • *"please bare with me"* - this isn't that kind of site – jonrsharpe Mar 16 '15 at 15:23
  • @jonrsharpe Hi there, perhaps show me some direction and tell me what you mean because I'm new and not sure how I should phrase a programming help request, sorry if this is not a good way of phrasing – Mariusz Andrzejewski Mar 16 '15 at 15:26
  • @TotempaaltJ thanks very much for the formatting suggestion, I had issues with this :) – Mariusz Andrzejewski Mar 16 '15 at 15:27
  • Hey @MariuszAndrzejewski, no problem! I just made some more minor edits cause I missed some spacing errors. Anyway, since you're just a beginner here's some tips: `myNumber.isnumeric() == True` in the `while` loop example you gave would be the same as just saying `myNumber.isnumeric()`. Secondly, your function names should be descriptive. "A", "B" and "X" don't tell me anything about what the functions do - try something like "random_digit" for the A function, for example. – Martijn Arts Mar 16 '15 at 15:30
  • @MariuszAndrzejewski just a joke - *"bear with me"* is the expression, *"bare with me"* suggests nudity! – jonrsharpe Mar 16 '15 at 15:31
  • @TotempaaltJ Thanks for this :) I gave them generic names because I thought it might be easier for people to read it 'clean' and not be influenced by what I called them as I guessed they only made sense in the context of my program (it's bigger than this) but in the future I can definitely just make new and descriptive names. Thanks for the help and hoping someone might be able to help me out soon! – Mariusz Andrzejewski Mar 16 '15 at 15:32
  • @jonrsharpe ahhhh freaked out because I thought you were thinking I was begging for free scripting when I've gone through the effort of spending 3 days writing this and still can't get the working program! Sorry, and funny joke, again, bear with me because I'm totally new to programming forums it seems! Thanks for the input :D – Mariusz Andrzejewski Mar 16 '15 at 15:33
  • I'll give you an upvote because I like how much effort you're putting into the question and following up! :D – Martijn Arts Mar 16 '15 at 15:44

1 Answers1

1

To answer both your questions:

  1. Loops, luckily, are easy. To loop over some code five times you can set tries = 5, then do while tries > 0: and somewhere inside the loop do a tries -= 1.

    If you want to get out of the loop ahead of time (when the user answered correctly), you can simply use the break keyword to "break" out of the loop. You could also, if you'd prefer, set tries = 0 so loop doesn't continue iterating.

  2. You'd probably want to put your validation inside the loop in an if (with the same statements as the while loop you tried). Only check if the input is valid and otherwise continue to stop with the current iteration of your loop and continue on to the next one (restart the while).

So in code:

answer = [random.randint(0, 9) for i in range(4)]

tries = 5
while tries > 0:
    number = input("Please enter the first 4 digit number: ")

    if not number.isnumeric() or not len(number) == len(answer):
        print('Invalid input!')
        continue

    out = ''
    for i in range(len(answer)):
        out += 'Y' if int(number[i]) == answer[i] else 'N'

    if out == 'Y' * len(answer):
        print('Good job!')
        break

    tries -= 1
    print(out)
else:
    print('Aww, you failed')

I also added an else after the while for when tries reaches zero to catch a failure (see the Python docs or maybe this SO answer)

Martijn Arts
  • 723
  • 7
  • 22
  • thanks for this help, I now get the structure but still am struggling to write the actual code for it. But you've help me realise how I need to structure it from your pseudocode/comments at the end there. – Mariusz Andrzejewski Mar 16 '15 at 16:00
  • Figured it wouldn't be fun if I just gave you the entire answer! If you _really_ want me to I can just add the if/else to fully answer the question though. ;) Otherwise, if you think I adequately answered your question, would you mind [marking it as the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)? – Martijn Arts Mar 16 '15 at 16:05
  • Hey again, I'm happy to mark this as the answer now because you've helped me with structure but I still don't have an answer to the question. I don't wish to seem greedy and take your time but if you are able to provide a solution please let me know, otherwise I guess I'll have to thank you for the structure you provided! – Mariusz Andrzejewski Mar 16 '15 at 16:13
  • 1
    I completed the answer. This is actually a fully functional script, but it doesn't use any functions. If you want to you, should split it up into functions. I think you'll be capable of doing that though :) – Martijn Arts Mar 16 '15 at 16:29
  • This is great for this section and currently integrating this into my own program. Just a question though, I'm unable to get lives to work. I'll keep working on it for now and let you know if I keep running into trouble. Thanks! – Mariusz Andrzejewski Mar 16 '15 at 17:04