-1

I'm trying to create a text based mini game. For the code of my loading screen, no matter what I input, I get "Shutting Down..." What am I doing wrong?

import random
import sys
def characterCreation():
    gender = raw_input("Please enter your gender: ")
    name = raw_input("Please enter your name: ")
    age = raw_input("Please enter your age: ")
def loadingScreen():
    option = raw_input("Welcome to Riften! Type either 'Play Game' or 'Quit' to procede!: ")
    option = str(option)
    if option == "Quit" or "quit" :
        sys.exit('Shutting down...')
    elif option == "Play Game" or "Play game" or "play Game" or "play game":
        characterCreation()
    else:
        while option != "Quit" or "quit" or "Play Game" or "Play game" or "play Game" or "play game":
            print("Please choose a valid option.: ")
            option = raw_input("Type either 'Play Game' or 'Quit' to procede!: ")
loadingScreen()

I wanna have it where if the user inputs "quit" in any capitalization, the program exits. If the user inputs "play game" the game continues to the character creation screen, and if the user inputs any thing else, to ask for the input of either "play game" or "quit" until given

Thank you

Rafael F
  • 47
  • 1
  • 6
  • 1
    Short answer: The use of `or` is wrong. – iBug Sep 26 '18 at 02:29
  • Code Review: Using `option.lower() == "quit"` will do a better job of handling input with "any capitalization". This has the free benefit of avoid the `or` statements that are seemingly confusing you. – Shadow Sep 26 '18 at 02:36

1 Answers1

2

You need to restate the variable name in each part of a comparison.

Your line if option == "Quit" or "quit" : is essentially bracketed out to
if (option == "Quit") or "quit". The string "quit" always evaluates to true, because it is truthy. Thus, the whole expression evaluates to true, and your condition is satisfied.

To correct this, you need to add the option to the second half of the comparison:
if option == "Quit" or option == "quit" :

The same goes for your while loop, except you'll actually want to make use of and, as you don't want option to equal any of the values specified:
while option != "Quit" and option != "quit" and option != "Play Game" and option != "Play game" and option != "play Game" and option != "play game":.

If you were to use or in your while loop, a similar problem would occur; each part of the codnition would be evaluated independently, and the entire condition would be satisfied if any of those components were satisfied. That is to say, if option was Quit, the first negative check (option != Quit) would fail, but the second check (option != quit) would pass, as Quit is not equal to quit. With an or statement, it doesn't matter that the first part already blocked this. With an and statement, it does, as all of the components must evaluate to true.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71