Python Forum
[Solved] Trying to rerun a snippet of code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Solved] Trying to rerun a snippet of code (/thread-37747.html)



[Solved] Trying to rerun a snippet of code - paracel - Jul-17-2022

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



RE: Trying to rerun a snippet of code - paracel - Jul-17-2022

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.



RE: Trying to rerun a snippet of code - deanhystad - Jul-17-2022

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.")



Solved: Trying to rerun a snippet of code - paracel - Jul-17-2022

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.")