Sunday, 15 August 2010

python - Loop and validation in number guessing game -


I have studied Visual Basic for the first application and slowly this week is happening with the dragon for speed That I am a new programmer, please stand with me I understand most of the concepts so far that I have faced, but am currently on a brick wall.

I have written some tasks that help me codify the game by guessing a number. User enters 4 digit number If it matches the programs generated by one (I have already coded it) then Y is added to the output list. If not, then an N.

Example I enter 4567, the number is 4568. The output printed from the list is YYYN.

  Import Random Diff A (): Points = Random. NumList = list (): numlist def x (): output = [] number = for numeric value (0, 9) return digit DEF (B): for counters in number (0,4) Input ("Please enter the first 4 digit number:") number2 = (I) in the range (0, lane (number)): If the number [i] == number 2 [i]: results.append ("Y") Second: results.append ("N") print (output) x ()  

I've coded all this but there are some things in it that cause it to be deficient:

< Ol>
  • I do not know how I can loop it So that I can ask it again. I just want to be able to guess that person 5 times, I'm imagining some type of loop with a counter like "1-5 counter, when I reach 5", but how unsure is it Program.

  • I have coded a standalone verification code snippet but I do not know how I can integrate it into a loop, for example if someone enters 444A So it should be said that this is not a valid entry and let them try again. I made an effort under it.

      while myNumber.isnumeric () == correct and lane (myNumber) == 4: for category (0, 4)): if myNumber [I] == progsNumber [i]: OutputList.append ("Y") and: outputList.append ("N")  
  • Trying to do some good effort, but it Everyone is struggling to patches together. Is anyone able to show me some direction to make me work together? I hope I've coded these basic elements that can help you to help!

    To answer your two questions:

    1. Loops, fortunately, are easy to do five times the loop on some code, you can set up tries = 5 , then try & gt; 0: and a attempts anywhere inside the loop - = 1 .

      If you want to get out of the loop before time (when the user answered correctly), you can simply use the break keyword for "brake" from the loop If you want, you can also do this, set tries = 0 , this loop does not release quickly.

    2. You probably want a if inside your loop (with the same statement the time you tried to loop) only check Make sure that the input is valid and otherwise continue to stop the current running of your loop and continue on to the next one (restart time).

    In the code:

      answer = [I try (4)] in the random (0, 9) category = 5 tries, while & gt; 0: number = input ("Please enter the first 4 digit number:") if the number is not. Numeric () or no lane (number) == lane (answer): print ('invalid input!') Continue = '' for I (in range (north)): outside + = 'y' if int [I]) == Answer [i] Other 'N' outside == 'Y' * Lane (Answer): Print ('Good job!') Print Print (out) Other: Print ('O, you fail' )  

    I still try to else added after some time to reach the zero to catch the failure (or maybe View)


    No comments:

    Post a Comment