Creating a "pop quiz"..

+1 Morten Nestander · September 29, 2015
Hi!
I have this assignment where we are supposed to make a test with questions and so on.
Ive started with this coding, but it is wrong.
What am i doing wrong and how would you make it?

Help?

Sorry for my broken English.


print("")
print()
print("What device is responsible for regenerating the signal so that the signal can travel a greater distance?")
print("A: Switch")
print("B. Router")
print("C. Hub")
print("D. Repeater")
print("E. Bridge")
answer = input("Your answer : ")
if answer == "D, d, repeater, Repeater" :
print ("Correct!")
if answer == "A"
print ("Wrong!")
if answer == "B" :
print ("Wrong!")
if answer == "C" :
print ("Wrong!")
if answer == "E" :
print ("Wrong!")


Post a Reply

Replies

Oldest  Newest  Rating
0 Owen Connors · November 9, 2015
Alternately you could make a function for the questions and pass the info in as an array
Like this:
def question(questionarray):
# Question(Question, Correct Answer)
ans = None
while ans is None:
print(questionarray[0])
ans = input(" >")[:1].lower()
if ans in questionarray[1]:
print("Correct!!!")
else:
ans = None
print("Incorrect, Try Again!")


# Questions
q1PossibleAnswers = ["d", "r"]
q1 = ["""What device is responsible for regenerating the signal so that the signal can travel a greater distance?
A. Switch
B. Router
C. Hub
D. Repeater
E. Bridge""", q1PossibleAnswers]
question(q1)


If you would like a walkthrough I could provide one, just ask :)
0 Owen Connors · November 9, 2015
Oh and add .strip after the [:1].lower
0 Halcyon Abraham Ramirez · September 29, 2015

choice = input("""What device is responsible for regenerating the signal so that the signal can travel a greater distance?
A: Switch
B. Router
C. Hub
D. Repeater
E. Bridge


your answer?: """).lower().strip()



if choice in ("d","repeater"):
    print("Correct")

else:
    print("Wrong")


you can use  multiline comments on an input to save you from using print alot. also since there is only one correct answer no need for unneccesary if staments an if and an else will suffice
+1 alex constantin · September 29, 2015
Also, you should do the same for each other if statement or simply assign answer to itself in lowercase:


print("")
print()
print("What device is responsible for regenerating the signal so that the signal can travel a greater distance?")
print("A: Switch")
print("B. Router")
print("C. Hub")
print("D. Repeater")
print("E. Bridge")
answer = input("Your answer : ")
formattedAnswer = answer.lower().strip()
if formattedAnswer == "d" or formattedAnswer == "repeater" :
print ("Correct!")
if formattedAnswer == "a" or formattedAnswer == "switch":
print ("Wrong!")
if formattedAnswer == "b" or formattedAnswer == "router":
print ("Wrong!")
if formattedAnswer == "c" or formattedAnswer == "hub":
print ("Wrong!")
if formattedAnswer == "e" or formattedAnswer == "bridge"
print ("Wrong!")
else:
print("Answer is not valid")
+1 alex constantin · September 29, 2015
With the line of code 

if answer == "D, d, repeater, Repeater" :

you are literally saying that the user must type in exactly "D, d, repeater, Repeater".
I guess what you are trying to achieve is that the answer is correct if it displays any of those, do this:


if answer.lower().strip() == "d" or answer.lower().strip() == "repeater":
   print("Correct!")


This code converts the input into lowercase and strips any unnecessary spaces and then checks if it is equal to "d" or "repeater"
  • 1

Python

107,285 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator