First, you can't possibly check for all possible uppercase/lowercase combinations of the answers, and it is looking messy even as it currently is. This is the perfect place to convert the input to lowercase like this:
q1 = input("What does LAN stand for?").lower()
Now, all you need to do is check for "hint" and "local area network" for question one:
if q1 == "hint":
print("The N stands for network...")
q1 = input("What does LAN stand for?").lower()
if q1 == "local area network":
correct()
else:
incorrect()
In question five, you have multiple inputs. Why don't you handle it like this instead of having multiple if
/elif
/else
statements?
if q5 == "hint":
print("A better one of these will boost grapical performance and framerate in games.")
q5 = input("Radeon, EVGA, XFX and Sapphire are companies that make what computer component?").lower()
if s in ['gpu', 'gpus', 'graphics card', 'graphics cards']:
correct()
else:
incorrect()
In this print
call, you should put spaces around your string literals and variables to make it easier to read:
print("You have",points,"points!")
q1
isn't a very descriptive variable name, and it does not hold the question, as it suggests. Also, why are you using one variable name for each question? This code can be cleaned up a lot by creating a method to administer the questions:
def administer_question(question, answers, hint):
while True:
user_answer = input(question)
if user_answer == "hint":
print(hint)
elif user_answer in answers:
correct()
return
else:
incorrect()
return
Now, the user can input "hint" as many times as they wish.
This is called as:
administer_question("Radeon, EVGA, XFX and Sapphire are companies that make what computer component?",
["gpu", "gpus", "graphics card", "graphics cards"],
"A better one of these will boost graphical performance and frame rate in games.")
I would pass a string to the incorrect()
function to display the correct answer to the user:
def incorrect(correct_answer):
print("Incorrect!")
print("The correct answer is '", correct_answer, "'")
pause = input()
Note also that I renamed the variable ab
to pause
so it demonstrates better that the program is just waiting for the user to input a value to continue. However, with all these pauses for input, maybe you should explain that to the user so they don't wait for the program to respond and wonder what is going on.
manpreet
Best Answer
2 years ago
This is my first program that uses more than just boring ol'
if
/else
statements - I made a whole 246 line game that's still a work in progress simply usingif
/else
- and I'm going to email this to my teacher later tonight.Please suggest more things I can add and report any bugs (although I've found none so far).