Reputation: 33
This is my code, why won't it work for me? It asks the questions but misses the if
statement that I have created.
print("What is your name?")
name = input("")
import time
time.sleep(1)
print("Hello", name,(",Welcome to my quiz"))
import time
time.sleep(1)
import random
ques = ['What is 2 times 2?', 'What is 10 times 7?', 'What is 6 times 2?']
print(random.choice(ques))
# Please note that these are nested IF statements
if ques == ('What is 2 times 2?'):
print("What is the answer? ")
ans = input("")
if ans == '4':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 10 times 7?'):
print("What is the answer? ")
ans = input("")
if ans == '70':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 6 times 2?'):
print("What is the answer? ")
ans = input("")
if ans == '12':
print("Correct")
else:
print("Incorrect")
import time
time.sleep(1)
import random
ques = ['What is 55 take away 20?', 'What is 60 devided by 2?', 'What is 500 take away 200']
print(random.choice(ques))
if ques == ('What is 55 take away 20?'):
print("What is the answer? ")
ans = input("")
if ans == '35':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 60 devided by 2?'):
print("What is the answer? ")
ans = input("")
if ans == '30':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 500 take away 200'):
print("What is the answer? ")
ans = input("")
if ans == '300':
print("Correct")
else:
print("Incorrect")
Upvotes: 0
Views: 906
Reputation: 121966
As well as the key error identified by MrHug, your code has the following problems:
import
;Note that the following code does what you are trying to do, but in a much more logical way:
# import once, at the top
import random
# use functions to reduce duplication
def ask_one(questions):
question, answer = random.choice(questions):
print(question)
if input("") == answer:
print("Correct")
else:
print("Incorrect")
# use proper string formatting
print("What is your name? ")
name = input("")
print("Hello {0}, welcome to my quiz".format(name))
# store related information together
ques = [('What is 2 times 2?', '4'),
('What is 10 times 7?', '70'),
('What is 6 times 2?', '12')]
ask_one(ques)
You could go further by storing the minimum information (i.e. the two numbers and the operator) in the list of ques
, then formatting the question and calculating the outputs in the code itself.
Upvotes: 1
Reputation: 1315
ques
is at all times still equal to the full list, not to a single element of the list.
If you want use this method, I suggest doing the following:
posedQuestion = random.choice(ques)
print(posedQuestion)
if posedQuestion == "First question":
elif ...
In addition you only need to do your imports once, so only one line saying import time
will do ;)
Upvotes: 2