Python Forum

Full Version: [Help] How to end While Loop using counter? {Screenshot attached}
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Please see the attached screenshot of my terminal to see what the issue is? ?

[Image: gran1_zpsxom4zzpe.png]

Ideally, the program should keep running if "BYE" is entered 3 times, not in a row/consecutive.

I'm lost what to add in my code so the program will ONLY end if "BYE" is entered 3 times in a row.

Thank you in advance.

=============================================================================================
Here's the exercise that I've been trying to do: ?

# Deaf grandma. Whatever you say to Grandma (whatever you type in), she should respond with this: HUH?! SPEAK UP, GIRL!

# unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back:

# NO, NOT SINCE 1938!

# To make your program really believable, have Grandma shout a different year each time, maybe any year at random between 1930 and 1950. (This part is optional and would be much easier if you read the section on Python’s random number generator under the Math Object.) You can’t stop talking to Grandma until you shout BYE.

# Hint: Try to think about what parts of your program should happen over and over again. All of those should be in your while loop.

# Deaf grandma extended. What if Grandma doesn’t want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times but not in a row, you should still be talking to Grandma.

=============================================================================================
Here's my code: ?

import random

no_goodbye = 0

while no_goodbye < 3:
	name = input("[Deaf Grandma] Hi! What is your name?: ")

	if name.isupper() is True:
		print("[Grandma shouting] NO, NOT SINCE" + " " + str(random.randint(1930, 1951)) + "!")
	else:
		print("[Grandma shouting] HUH?! SPEAK UP, GIRL!!!")

	if name == "BYE":
		print("[Grandma shouting] I STILL WANT TO TALK TO YOU!")
		no_goodbye += 1
This should work. I just added two lines.
By the way, name is a really bad name.

import random

no_goodbye = 0

while no_goodbye < 3:
    name = input("[Deaf Grandma] Hi! What is your name?: ")

    if name.isupper() is True:
        print("[Grandma shouting] NO, NOT SINCE" + " " + str(random.randint(1930, 1951)) + "!")
    else:
        print("[Grandma shouting] HUH?! SPEAK UP, GIRL!!!")

    if name == "BYE":
        print("[Grandma shouting] I STILL WANT TO TALK TO YOU!")
        no_goodbye += 1
    else:
        no_goodbye = 0
When there is no 'BYE', no_goodbye is set to 0.
I even can't write a shorter english sentence, as the code describes it.
OMG! You helped me solve something which took me hours to figure out as a beginner. lol

Thank you so much, DeaD_EyE! Heart Heart Heart

Here's my terminal screenshot: Dance

[Image: gran3_zps1chhbsr6.png]