Python Forum
[Solved] Trying to rerun a snippet of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Trying to rerun a snippet of code
#1
Hello, I'm still fairly new to Python3. I'm tyring to figure out the best way to rerun this code when a key ins't pushed correctly. Appreciate any help thanks.
#Code to exit or reginirate script
quittogen = input("""\nDo you want to quit or generate another creature?
 Type q to quit or g to generate a new creature.""")

if str(quittogen) == "q" or str(quittogen) == "Q":
    input("Thank you for using the Fantasy Creature Generator.")
elif str(quittogen) == "g" or str(quittogen) == "G":
    print("Generating New Creature\n")
else:
    if str(quittogen) != "q" or str(quittogen) != "g" or \
	str(quittogen) != "Q" or str(quittogen) != "Q":
        quittogen = input("""\nDo you want to quit or generate another
 creature? Type q to quit or g to generate a new creature.""")
 #Need a else code here to repeat the whole upper code
Reply
#2
Ive been messing with this and I think I am getting closer, this is the code I am using so far but I'm getting a error. "Un-ident does not match any outer indentation level. Trying to figure it out. Think I am getting closer.

#Code to exit or reginirate script
running = True
while running:
	quittogen = input("""\nDo you want to quit or generate another 
creature?\n Type q to quit or g to generate a new creature.""")
    if str(quittogen) == "q" or str(quittogen) == "Q":
		print("Thank you for using the Fantasy Creature Generator.")
        running = False
    elif str(quittogen) == "g" or str(quittogen) == "G":
        print("Generating New Creature\n")
    else:
        print("\nPlease enter 'g' to start over or 'q' to quit.
Reply
#3
I get an error because the string in the last print is open ended (no closing quote). I do not get an indent error, but that is no surprise since posting code usually fixes problems like mixing tabs and spaces. My solution. It allows "Quit" and "generate".
while True:
    print("\nDo you want to quit or generate another creature?")
    reply = input("Type q to quit or g to generate a new creature. ")
    if reply:
        if reply[0] in ("g", "G"):
            print("Generating New Creature\n")
            continue
        elif reply[0] in ("q", "Q"):
            print("Thank you for using the Fantasy Creature Generator.")
            break
        print("\nPlease enter 'g' to start over or 'q' to quit.")
paracel likes this post
Reply
#4
Thanks, I managed to get my code working after I deleted all the spaces and replaced them again on the idents and put the closing bracket. I do like the way your code works better however little cleaner. Thanks for the help.

(Jul-17-2022, 04:59 AM)deanhystad Wrote: I get an error because the string in the last print is open ended (no closing quote). I do not get an indent error, but that is no surprise since posting code usually fixes problems like mixing tabs and spaces. My solution. It allows "Quit" and "generate".
while True:
    print("\nDo you want to quit or generate another creature?")
    reply = input("Type q to quit or g to generate a new creature. ")
    if reply:
        if reply[0] in ("g", "G"):
            print("Generating New Creature\n")
            continue
        elif reply[0] in ("q", "Q"):
            print("Thank you for using the Fantasy Creature Generator.")
            break
        print("\nPlease enter 'g' to start over or 'q' to quit.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 386 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  [Solved]Help to iterate through code Extra 7 1,175 Dec-11-2022, 10:55 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,250 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,071 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  English interpretation of the following file handing snippet mortch 5 3,192 May-30-2019, 08:10 AM
Last Post: mortch
  How to rerun script again SteampunkMaverick12 2 2,373 Apr-30-2019, 03:57 PM
Last Post: SteampunkMaverick12
  python code snippet chinna 4 2,673 Mar-15-2019, 06:26 AM
Last Post: chinna
  How to rerun the program in a loop with different input? bharaths 3 3,782 Nov-23-2018, 09:36 AM
Last Post: bharaths
  Unable to read csv file inside full code snippet, at the same time its working seprat sreeraj 2 3,027 Jan-28-2018, 07:25 PM
Last Post: sreeraj

Forum Jump:

User Panel Messages

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