Probably a stupid question

Joined
May 4, 2023
Messages
1
Reaction score
0
I'm in highschool and in a basic computer science class for coding and I am working on a simple coded game for my final, its a text based escape room style game, and I am having trouble with my ifs and else statements, I am trying to put in passwords for safes and stuff and I am having issues with incorrect inputs, whenever there is an incorrect input it seems to disregard the else statement and move forward, I am trying to get it to loop back around for another try.
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
Here is a quick example of testing whether to execute the if or else statements.

Python:
import os # Import to use the terminal clear

# Clear terminal text
def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

class BlankSpaceException(Exception):
    '''
        Create an eception error for handling blank space entered
    '''
    pass

# Variables for formatting terminal text. May not work on all systems
BOLD = '\033[1m'
END = '\033[0m'
CYAN = '\033[96m'

# Set run variable to true
run = True

# List of words used to end the program
words = ['quit', 'exit', 'bye', 'goodbye']

clear() # Call the clear function to clear terminal text

# Start the while loop
while run:
    '''
        Using a try block to halt the loop for user input
    '''
    try:
        # Print a message showing how to exit
        print(f'To exit program type one of the following: {", ".join(words)} and press enter.')
        
        # Get user input
        ask = input('Type something: ')

        # If user input is blank raise exception error
        if len(ask.strip()) < 1:
            raise BlankSpaceException

        # If user input is in the exit words, set run to False and exit
        if ask.lower() in words:
            clear()
            print('Stopping program... Goodbye.')
            run = False
        else:
            # Print user input
            clear()
            print(f'\nYou entered: {BOLD + CYAN + ask + END}\n')

    except BlankSpaceException:
        clear()
        print('You entered a blank space. Nothing to print.\n')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top