Python Forum

Full Version: Name guessing game in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys so I am having some trouble with the second part of a program that I need to write 

So the first part was to write a number guessing game in which the user has a limited number of tries to guess the number between 1 and 10 in which I have managed to do but now we have to alter the code so it can Write a program that asks the user to guess your name using a while loop. I need to repeatedly ask the user for a name and when they input your name e.g John, it displays "Got it" and stops looping.  if it's not your name it outputs "Guess again".


Also it has to be able to handle input in upper and lower case.


So far my number guessing game is looking like this.
import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 10.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 10)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1
    if tries == 6:
        print ("You failed to guess in time!")
        break
    if guess == the_number:
        print("You guessed it! The number was", the_number)
        print("And it only took you", tries, "tries!\n")
So far I have something like this that is very basic
name = ''

while name != 'Kate':
    print('What is my name?')
    name = input()

print('Yes, My name is ' + name + '. Congratulations')
Also I have managed to modify it somewhat. I am just confused to why when you guess the correct name it wont print Got it instead of Try again...

name = ''

while name != 'Kate':
    print('What is my name?')
    name = input()
if input == name:
    print("Got it")
else:
    print("Try Again")
Output:
What is my name?
Jack
What is my name?
John
What is my name?
Kate
Try Again

Process finished with exit code 0
Probably something quite simple I am missing here lol
if input == name: is comparing a function object (input) to a variable that contains a string (name) (where name received the result of input()).
So I managed to get it to print "Got it" when user inputs the correct name but how do I get it to print "Guess again" when you enter an incorrect name?

name = ''

while name != 'Kate':
   print('What is my name?')
   name = input()
if name == name:
   print("Got it")
else:
   print("Try Again")
Managed to figure this one out myself. I had the order of operation wrong by the looks of it

name = ''

print('What is my name?')
name = input()
while name != 'Kate':
     print("Guess Again")
     print('What is my name?')
     name = input()
while name == 'Kate':
   if name == 'Kate':
      print("Got it!")
   break
The only thing now is to make the correct answer be accepted in any case format such as KATE or KaTe. Anyone know how to do this? Cheers.
The lower method if strings is the typical way to do case-insensitive checks (that is, to catch any case format):
while name.lower() != 'kate':
It converts all upper case characters in the string to lower case.
(Apr-01-2017, 10:51 AM)ichabod801 Wrote: [ -> ]The lower method if strings is the typical way to do case-insensitive checks (that is, to catch any case format):
 while name.lower() != 'kate': 
It converts all upper case characters in the string to lower case.


Ahh genius thanks heaps works now :)

name = ''

print('What is my name?')
name = input()
while name.lower() != 'kate':
     print("Guess Again")
     print('What is my name?')
     name = input()
while name.lower() == 'kate':
   if name.lower() == 'kate':
      print("Got it!")
   break
At the end, the while loop and the if statement are redundant. They both check the same condition. So you only need one (the if statement would be more appropriate).