-2
"""
GuessNumber.py - This program allows a user to guess a number
                  between 1 and 10.
Input:  User guesses numbers until they get it right.
Output: Tells users if they are right or wrong.
"""

import random

number = random.randint(1, 10)

# Prime the loop.
keepGoing = input("Do you want to guess a number? Enter Y or N ")

# Validate input.

# Enter loop if they want to play.
while keepGoing == "Y":
    # Get user's guess.
    stringNumber = input("I'm thinking of a number. .\n Try to guess by entering a number between 1 and 10 ")
    userNumber = int(stringNumber)
    # Validate input.


    # Test to see if the user guessed correctly.
    if userNumber == number:
        keepGoing = "N"
        print("You are a genius. That's correct!")
    else:
        keepGoing = input("That's not correct. Do you want to guess again? Enter Y or N ")
        # Validate input.

If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise the program asks the user if he or she wants to guess again. If the user enters "Y", he or she can guess again. If the user enters "N", the loop exits. You can see that the "Y" or "N" is the sentinel value that controls the loop. I need to ass a code that validates "y" and "n"

2 Answers2

0

keepGoing.upper() == "Y" would check lower and upper case inputs.

You shouldn't need to validate N/n

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
import random
number=random.randint(1,10)
keepGoing=raw_input("DO you want to guess a number:\n ")
keepGoing=keepGoing[0].upper()
while keepGoing=='Y':
    stringNumber=int(raw_input("I'm thinking of a number. .\n Try to guess by   entering a number between 1 and 10:\n "))
    if (stringNumber==number):
        print("You are a genius. That's correct!")
        break
    else:
        keepGoing=raw_input("DO you want to guess a number")
        keepGoing=keepGoing[0].upper()