Python Forum
Name guessing game in python
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Name guessing game in python
#1
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
Reply
#2
if input == name: is comparing a function object (input) to a variable that contains a string (name) (where name received the result of input()).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
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")
Reply
#4
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.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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
Reply
#7
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Play again, in a guessing game banidjamali 4 11,629 Jan-22-2021, 06:23 AM
Last Post: banidjamali
  Help - random number/letter guessing game juin22 1 3,182 Aug-16-2020, 06:36 AM
Last Post: DPaul
  making a guessing number game blacklight 1 2,183 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guessing game with 4 different digits LinseyLogi 6 3,606 Mar-29-2020, 10:49 AM
Last Post: pyzyx3qwerty
  Guessing game with comparison operators Drone4four 9 13,730 Dec-02-2018, 06:12 PM
Last Post: ichabod801
  Guessing Game "limit 4 attempts" help rprollin 3 19,705 Jun-23-2018, 04:37 PM
Last Post: ljmetzger
  guessing script simon 3 5,544 Oct-10-2016, 01:47 PM
Last Post: simon
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,816 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020