logo codesdope

Help with a Python program for an absolute beginner

   DeanTheGreatAndPowerful

Alright, so to start, I’m very new to Python. Like, incredibly new. I don’t know much.

So I’m trying my hand at creating a Hogwarts house sorting quiz. It’s working out rather well except that I can’t seem to find a way to terminate the quiz when the user inputs an answer besides 1, 2, 3, or 4. I’ve tried doing “if q1 != ‘1’ or ‘2’ or ‘3’ or ‘4’”, but that isn’t working out.

Here’s my code:

import time
import sys
#Initially setting the values for each of the four houses to 0
G = 0
R = 0
H = 0
S = 0
print("Welcome, new student at Hogwarts! What is your name?")
name = input()
print("Well,",name,", we're very excited for you to join us!") #Introduction with name
time.sleep(2)
print("Today, you are going to be sorted into your Hogwarts house.")
time.sleep(2)
print("Your house will be like your family for the next 7 years. Your triumphs will earn your house points and your mistakes will lose them points.")
time.sleep(4)
print()
print("To determine your house, you will be asked a series of questions. To answer, please type in the number that is paired with your desired answer")
time.sleep(4)
print()
print("First off, what would you most enjoy doing on a lazy day?")
time.sleep(0.25)
print("1: Pull some pranks and finalize your plan for world domination")
time.sleep(0.25)
print("2: Volunteer at your local animal shelter. It's hard work, but it's worth it")
time.sleep(0.25)
print("3: Curl up with a good book")
time.sleep(0.25)
print("4: Go out and fight a dragon or something")
time.sleep(4)
q1 = input("Which do you choose? >>> ")
#Check the answer to see which house to add point to
if q1 == "1":
    S=S+1 #Add 1 point to Slytherin's current score
if q1 == "2":
    H=H+1 #Add 1 point to Hufflepuff's current score
if q1 == "3":
    R=R+1 #Add 1 point to Ravenclaw's current score
if q1 == "4":
    G=G+1 #Add 1 point to Gryffindor's current score
if q1 != "1" or "2" or "3" or "4":
    print("That isn't a valid answer. Apparently, you're either a squib or a very nosy muggle. Prepare to have your memory wiped")
    time.sleep(2)
    sys.exit()
print()
time.sleep(0.5)
print("Wonderful. Second question: You see a kid being bullied. How do you respond?")
time.sleep(0.25)
print("1: Find a teacher right away")
time.sleep(0.25)
print("2: Expose the bully's inferior intellect by challenging them to a battle of wits")
time.sleep(0.25)
print("3: Confront the bully and have a glorious duel with them!")
time.sleep(0.25)
print("4: Keep away from the scene and plan to get back at the bully by psychologically torturing them and driving them to the brink of insanity")
time.sleep(4)
q2 = input("Your answer >>> ")
#Check the answer to see which house to add point to
if q2 == "1":
    H=H+1 #Add 1 point to Hufflepuff's current score
if q2 == "2":
    R=R+1 #Add 1 point to Ravenclaw's current score
if q2 == "3":
    G=G+1 #Add 1 point to Gryffindor's current score
if q2 == "4":
    S=S+1 #Add 1 point to Slytherin's current score
if not(q2 == "1" or "2" or "3" or "4"):
    print("That isn't a valid answer. You must be either a squib or a very nosy muggle. Somehow you managed to get past the first question, but there's no fooling the Sorting Hat. Prepare to have your memory wiped")
    time.sleep(2)
    sys.exit()
print()
time.sleep(0.5)
print("You're doing great. Third question: Which would you rather be?")
time.sleep(0.25)
print("1: A great wolf, able to challenge even the best combatants")
time.sleep(0.25)
print("2: Merely a dutiful songbird, working hard to build my nest, provide for my family, and make the world a little happier")
time.sleep(0.25)
print("3: A cunning spider, able to trick and trap the foolish in my web")
time.sleep(0.25)
print("4: A sharp-minded crow, travelling the world and learning its knowledge")
time.sleep(4)
q3 = input("Your answer >>> ")
#Check the answer to see which house to add point to
if q3 == "1":
    G=G+1 #Add 1 point to Gryffindor's current score
if q3 == "2":
    H=H+1 #Add 1 point to Hufflepuff's current score
if q3 == "3":
    S=S+1 #Add 1 point to Slytherin's current score
if q3 == "4":
    R=R+1 #Add 1 point to Ravenclaw's current score
if q3 != "1" or "2" or "3" or "4":
    print("That isn't a valid answer. Do you really feel the need to play around with this quiz? You must be either a squib or a very bothersome muggle. Prepare to have your memory wiped")
    time.sleep(2)
    sys.exit()
print()
time.sleep(0.5)
print("Two questions left! Fourth question: In a battle, where would you be?")
time.sleep(0.25)
print("1: In the infirmary, treating the injured and giving everyone moral support")
time.sleep(0.25)
print("2: In the back, strategizing the plan of attack")
time.sleep(0.25)
print("3: Out on the front lines, fighting my hardest")
time.sleep(0.25)
print("4: Acting as a spy and gaining information from the enemy")
time.sleep(4)
q4 = input("Your answer >>> ")
#Check the answer to see which house to add point to
if q4 == "1":
    H=H+1 #Add 1 point to Hufflepuff's current score
if q4 == "2":
    R=R+1 #Add 1 point to Ravenclaw's current score
if q4 == "3":
    G=G+1 #Add 1 point to Gryffindor's current score
if q4 == "4":
    S=S+1 #Add 1 point to Slytherin's current score
if q4 != "1" or "2" or "3" or "4":
    print("That isn't a valid answer. I am disappointed in you. You must be either a squib or a very bothersome muggle. Prepare to have your memory wiped")    
    time.sleep(2)
    sys.exit()
print(G,R,H,S)

The first one is at line 40. Then, at line 66 I tried something different. And at 93 and 118 it’s the same as 40.

I know that I’m probably making some obvious mistake, so could anyone point it out to me?


Answers

  •    pedromik
    if q1 != "1" and q1 != "2" and q1 != "3" and q1 != "4":

    that means if your input is “1” : if False and True and True > this gives False finally;

    you can also write something like:if q1 not in ["1", "2", "3", "4"]:

    in this part of your code:

    q1 != "1" or "2" or "3" or "4":

    q1 != "1" gives you True or False (depends on input)

    so you have True or “2” or “3” or “4” gives you True

                      False or “2” or “3” or “4” gives you “2”



Write your answer
Close

Welcome.please sign up.

Close

Welcome.please login.