Python Forum
Ask again if wrong guess with While.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ask again if wrong guess with While.
#1
Hi

I'm learning python and exercising the loop function with while.

I would like to make a code that ask me to input the name again if it's wrong.. with no limit of tries.

name = "Ed"
guess = input("what is the name")

while guess == name:
    print(guess, 'is the right name')
    break
if guess != name:
    print(input(guess))

Wall
Yoriz write Oct-21-2021, 04:46 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You need to get the users input and check if the input is correct inside of the while loop and only break from the loop when it is correct.
Reply
#3
Oh Thank you, i got it work now.
Is there another way to do it more simple than

name = "Ed"

while name:
    guess = input("what is the name")
    if guess != name:
     print(guess, 'is wrong')
    else: 
     print(guess, "is the right name")
     break
Reply
#4
while name will always be True so you may as well just use while True.
Indentation should be 4 spaces.
You can use f strings.
if name is a constant use capital letters otherwise lowercase is ok.
NAME = "Ed"

while True:
    guess = input("what is the name")
    if guess != NAME:
        print(f"{guess} is wrong")
    else:
        print(f"{guess} is the right name")
        break
EduPy likes this post
Reply
#5
If you would like to check against a list of names:

#! /usr/bin/env python3.9

names = ['john', 'mary', 'bob', 'sue']

while True:
    name = input('What is the name: ').lower()
    if name not in names:
        print('That name is not in the list.')
        continue
    else:
        print('That name is in the list.')
        break
Output:
What is the name: charlie That name is not in the list. What is the name: bob That name is in the list.
EduPy likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 2,960 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  I guess it's about print tsavoSG 2 2,077 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  python gives wrong string length and wrong character thienson30 2 2,943 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  can't figure out problem with number guess Galoxys 4 3,293 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess a number Ameen 5 13,070 Apr-03-2018, 01:20 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,096 Jan-07-2018, 04:26 AM
Last Post: squenson
  "Guess the Number" game CTT 14 13,220 Jul-26-2017, 11:41 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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