Can anyone help me find out whats wrong with my code? something isnt right but python says its fine

Question:

The code is functioning. Python says the code is executing, but it wont print anything. Ive looked around and i cant find anything that answers my question. why wont the code print the print statements? Ive only been learning python on the ocr gcse specification for about 2yrs now so im not the most advanced. Ive had to do extra reserch on things ive already learned to get where i am now. This project is due on the morning of 27.3.23 and its currently the evening of 24.3.23. i only have 3 days to complete it and i dont know why the code wont work. any help will be highly appreciated.

the code i currently have is Here:

#imports needed for game

import sys
import os


#sets a class for the player
class player:
    def __init__(self):
       self.name=' '
       self.hp = 0
       self.sa = []
   
#title screen
def title_selections():
    option = input("> ")
    if option.lower() == ("play"):
         start_game() #place holder until written
    
    elif option.lower == ("help"):
            help_screen()
        
    elif option.lower() == ("quit"):
        sys.exit()
    
    while option.lower() not in ['play','help','quit']:
        def title_selections():
            option = input("> ")
        if option.lower() == ("play"):
            start_game() #place holder until written
    
        elif option.lower == ("help"):
            help_screen()
        
        elif option.lower() == ("quit"):
             sys.exit()
#outputted title screen

def title_selections():
    os.system('clear')

    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('       -Play          ')
    print('       -Help          ')
    print('       -Quit         ')

#help screen:
def help_screen():
    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('-The DM (Dungeon Master) will ask you questions.')
    print('-And as the player, it is your choice to decide what your character will do.')
    print('-In certain situations, you will be required to answer these questions with              great thought!')
    print('-So remember to think carefully when answering these questions.')
    print('-Or any questions for that matter!')
    print('-Proceed with caution, and remember to have fun!')
Asked By: user21484628

||

Answers:

Three things are wrong here:

  1. You define title_selections twice
  2. You define a function within an if-statement (inside the first title_selections
  3. You actually never call any of your functions.

What you probably want is this:


#imports needed for game

import sys
import os


#sets a class for the player
class player:
    def __init__(self):
       self.name=' '
       self.hp = 0
       self.sa = []
   
#title screen
def handle_selections():
    option = input("> ")
    if option.lower() == ("play"):
         start_game() #place holder until written
    
    elif option.lower == ("help"):
            help_screen()
        
    elif option.lower() == ("quit"):
        sys.exit()
    
    if option.lower() not in ['play','help','quit']:
        print_selections()
        handle_selections()
#outputted title screen

def print_selections():
    os.system('clear')

    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('       -Play          ')
    print('       -Help          ')
    print('       -Quit         ')

#help screen:
def help_screen():
    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('-The DM (Dungeon Master) will ask you questions.')
    print('-And as the player, it is your choice to decide what your character will do.')
    print('-In certain situations, you will be required to answer these questions with              great thought!')
    print('-So remember to think carefully when answering these questions.')
    print('-Or any questions for that matter!')
    print('-Proceed with caution, and remember to have fun!')

print_selections()
handle_selections()
Answered By: SuperEwald

I noticed a few issues here.

  1. title_selections is defined twice – I solved this by moving the code from the 2nd definition into the top of the first
  2. Functions are never called – I called title_selections() at the bottom of the code
  3. On the two lines where you check if the input is "help", you are missing the parentheses in option.lower() – I added the parentheses to both option.lower() calls
  4. You define a function inside of an if statement inside of another function – I removed the function definition

Here’s the code all fixed up:

# imports needed for game

import sys
import os


# sets a class for the player
class player:
    def __init__(self):
        self.name = ' '
        self.hp = 0
        self.sa = []


# title screen
def title_selections():
    os.system('clear')

    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('       -Play          ')
    print('       -Help          ')
    print('       -Quit         ')

    option = input("> ")
    if option.lower() == ("play"):
        # start_game()  # place holder until written
        x = 0
    elif option.lower() == ("help"):
        help_screen()

    elif option.lower() == ("quit"):
        sys.exit()

    while option.lower() not in ['play', 'help', 'quit']:
        option = input("> ")

        if option.lower() == ("play"):
            # start_game()  # place holder until written
            x = 0
        elif option.lower() == ("help"):
            help_screen()

        elif option.lower() == ("quit"):
            sys.exit()


# help screen:
def help_screen():
    print('######################')
    print('#Welcome adventurer!#')
    print('######################')
    print('-The DM (Dungeon Master) will ask you questions.')
    print('-And as the player, it is your choice to decide what your character will do.')
    print('-In certain situations, you will be required to answer these questions with              great thought!')
    print('-So remember to think carefully when answering these questions.')
    print('-Or any questions for that matter!')
    print('-Proceed with caution, and remember to have fun!')


title_selections()


Answered By: jed331
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.