Python Forum
[Help] How to end While Loop using counter? {Screenshot attached}
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] How to end While Loop using counter? {Screenshot attached}
#1
Question 
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
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting For Loop Counter rturus 2 792 Dec-07-2022, 01:34 PM
Last Post: deanhystad
  IEDriverServer screenshot ABVSVL 0 1,735 Jul-12-2020, 09:31 AM
Last Post: ABVSVL
  pyautogui.screenshot region is not working alexlu 6 6,526 Jun-04-2020, 10:46 AM
Last Post: alexlu
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,616 May-15-2020, 12:37 PM
Last Post: scidam
  Spawning a new process that is not attached to python cman234 3 1,922 Apr-25-2020, 05:24 PM
Last Post: cman234
  My objective is to get the shape parameters of the particles in attached image chad221 0 1,790 Oct-26-2019, 10:27 AM
Last Post: chad221
  Want to take a Screenshot from a File in Linux dhiliptcs 2 2,568 Oct-21-2019, 01:22 PM
Last Post: dhiliptcs
  Parsing Attached .MSG Files with Python3 ericl42 1 3,684 Apr-12-2019, 06:28 PM
Last Post: ericl42
  Screenshot of specific window kainev 10 19,354 Nov-30-2018, 03:07 PM
Last Post: kainev
  datetime with every screenshot name evilcode1 7 5,536 Aug-27-2018, 06:01 PM
Last Post: buran

Forum Jump:

User Panel Messages

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