Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Loop question
#1
Hi,

So I am writing a very basic text based game, and have put a loop withing a loop for depending upon which game branch I go down. I was wondering how I can break out of both loops, or perhaps the code I have written is a bad way to do this. (I have simplified my code to give you up to where I want to break.

Many thanks!!

def game():
    print("Welcome to the game.")
    print("You see a robber in the courtyard. What do you want to do?\n")

attack = ["scream","shout","attack","hit","punch","kick"]

while True:
    game()

    courtyard_decision = input(">>").lower().split()



    if set(courtyard_decision) & set(attack):
        print("You have decided to get into a fight with the Robber")
        while True:
            print("""The robber dodges your first attack.
            He then throws a punch and you slip to the left.
            Would you like to throw a left or right hook?""")
            fight = input(">>>").lower().split()
            if "left" in fight or "right" in fight:
                print("Your hook hits the robber and you have completed the game!")
                break
Reply
#2
Non-pythonic quick fix is to set a variable:

proceed = True
while proceed:
            print("""The robber dodges your first attack.
            He then throws a punch and you slip to the left.
            Would you like to throw a left or right hook?""")
            fight = input(">>>").lower().split()
            if "left" in fight or "right" in fight:
                print("Your hook hits the robber and you have completed the game!")
                proceed = False
                break
Correct way is to write function for first loop and in second loop deal only with results of said function.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks for your reply, but this does not seem to work for me. Its returns me to the first loop when I want to be breaking from them both.
Reply
#4
(Jul-24-2018, 12:04 PM)BigDisAok Wrote: Thanks for your reply, but this does not seem to work for me. Its returns me to the first loop when I want to be breaking from them both.

Mea culpa! I was not focused enough. If you put it before first while loop it should work. Second while loop is exited by break
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
a useful read before you dive in the if/else nightmare text adventure game
https://python-forum.io/Thread-Text-Adve...dictionary
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Buran, thanks for the link. Interesting read as creating dictionary's will be needed to shorten my programming required!! Will put this on my to learn list :)

and thanks perfringo, that works perfectly for leaving both loops. Exactly what I wanted!

:)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  A question about 'Event loop is closed' fc5igm 2 2,165 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Beginner question NameError amazing_python 6 2,379 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,137 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  for loop question KEYS 1 1,697 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,265 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  while loop question KEYS 2 1,976 Sep-26-2020, 11:02 PM
Last Post: KEYS

Forum Jump:

User Panel Messages

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