patternpythonModerate
Creating a questionnaire
Viewed 0 times
creatingquestionnairestackoverflow
Problem
Im making a Python program. It is basically a questionnaire and I was just wondering if there is a shorter way of doing this as at the moment I'm writing out all of the questions in separate functions. I was wondering if there is an alternative way of doing this in almost a single function where the function just repeats but the question and right answers change every time. If I cannot do this is there an alternate way to do this without having to do separate functions for each question? is there a faster/more efficient way of completing this?
All advice would be much appreciated and I have pasted the first 4 questions and the rest of the code below. I will be having many more questions but i just did 4 so that it wouldn't take up too much space :)
```
import time
questionNumber = 0
right = 0
wrong = 0
name = str(input("Please Enter your name"))
print()
def questions():
def questionOne():
global right, wrong, questionNumber
print("What is the population of New Zealand?")
print("Is it A:6.7m B:3.2m C:5.1m or D:4.5m")
ans = str(input())
if ans == "D" or ans == "d" or ans == '4.5' or ans == '4':
print("You got it right!")
right = right + 1
else:
print("You got it wrong!")
wrong = wrong+1
questionNumber = questionNumber + 1
questionOne()
time.sleep(2)
print()
print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions")
print()
time.sleep(4)
def questionTwo():
global right, wrong, questionNumber
print("What year did the first european set foot on New Zealand (Abel Tasman)")
print("Is it A:1830 B:1543 C:1642 or D:1765")
ans = str(input())
if ans == "C" or ans == "c" or ans == '1642'or ans == '3':
print("You got it right!")
right = right + 1
else:
print("You got it wrong!")
All advice would be much appreciated and I have pasted the first 4 questions and the rest of the code below. I will be having many more questions but i just did 4 so that it wouldn't take up too much space :)
```
import time
questionNumber = 0
right = 0
wrong = 0
name = str(input("Please Enter your name"))
print()
def questions():
def questionOne():
global right, wrong, questionNumber
print("What is the population of New Zealand?")
print("Is it A:6.7m B:3.2m C:5.1m or D:4.5m")
ans = str(input())
if ans == "D" or ans == "d" or ans == '4.5' or ans == '4':
print("You got it right!")
right = right + 1
else:
print("You got it wrong!")
wrong = wrong+1
questionNumber = questionNumber + 1
questionOne()
time.sleep(2)
print()
print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions")
print()
time.sleep(4)
def questionTwo():
global right, wrong, questionNumber
print("What year did the first european set foot on New Zealand (Abel Tasman)")
print("Is it A:1830 B:1543 C:1642 or D:1765")
ans = str(input())
if ans == "C" or ans == "c" or ans == '1642'or ans == '3':
print("You got it right!")
right = right + 1
else:
print("You got it wrong!")
Solution
Some PEP8(which is a Style Guide for Python Code) comments regarding your coding style and formatting:
Between methods, you should have two blank lines.
In PEP8 it's recommended to:
Use the function naming rules: lowercase with words separated by
underscores as necessary to improve readability.
That said,
I've seen many people using also
The same rule applies for your methods:
Put a space after each comma (
So this:
Might be rewritten as:
Instead of
More, instead of doing this:
You can do:
I don't like how you formatted the above
Let's see what we've got so far using the modifications I mentioned so far:
Digging deeper:
Globals
Try to avoid using globals as much as possible. Global variables should be avoided because they inhibit code reuse. The reason they are bad is that they allow functions to have hidden (as in "non-obvious" and "undeclared") and thus hard to understand side effects. Also, this can lead to Spaghetti code.
Duplicate code
As you can see, your functions are pretty much the same, so you can use this and try making a single method. More, we can get rid of that ugly usage of inner methods. It's just not necessary in this case.
What I'd do is:
- Spacing
Between methods, you should have two blank lines.
def questionOne():
....
def questionTwo():
....- Naming conventions
In PEP8 it's recommended to:
Use the function naming rules: lowercase with words separated by
underscores as necessary to improve readability.
That said,
questionNumber would become question_numberI've seen many people using also
camelCase for variables, so that's rather a matter of preference.The same rule applies for your methods:
questionOne() -> question_one()Put a space after each comma (
,). It really helps when going through your code.So this:
print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")Might be rewritten as:
print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed",
questionNumber, "Questions\n")- Other comments
Instead of
right = right + 1 you can use an augmented assignment: right += 1.More, instead of doing this:
print()
print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions")
print()You can do:
print("\nSo far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")I don't like how you formatted the above
print() but I'll come back to it later on.Let's see what we've got so far using the modifications I mentioned so far:
import time
wrong = 0
right = 0
question_number = 0
name = str(input("Please Enter your name: "))
def questions():
def question_one():
global right, wrong, question_number
print("What is the population of New Zealand?")
print("Is it A:6.7m B:3.2m C:5.1m or D:4.5m")
ans = str(input())
if ans == "D" or ans == "d" or ans == '4.5' or ans == '4':
print("You got it right!")
right += 1
else:
print("You got it wrong!")
wrong += 1
question_number += 1
question_one()
time.sleep(2)
print("\nSo far", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed",
question_number, "Questions\n")
time.sleep(4)
def question_two():
global right, wrong, question_number
print("What year did the first european set foot on New Zealand (Abel Tasman)")
print("Is it A:1830 B:1543 C:1642 or D:1765")
ans = str(input())
if ans == "C" or ans == "c" or ans == '1642' or ans == '3':
print("You got it right!")
right += 1
else:
print("You got it wrong!")
wrong += 1
question_number += 1
question_two()
time.sleep(2)
print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed",
question_number, "Questions\n")
time.sleep(4)
def question_three():
global right, wrong, question_number
print("How many Kiwi are there left in New Zealand (Approx)")
print("Is it A:2000 B:600 C:70,000 or D:100000")
ans = str(input())
if ans == "D" or ans == "d" or ans == '100000' or ans == '4':
print("You got it right!")
right += 1
else:
print("You got it wrong!")
wrong += 1
question_number += 1
question_three()
time.sleep(2)
print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed",
question_number, "Questions\n")
time.sleep(4)
def question_four():
global right, wrong, question_number
print("How many new babys where born in New Zealand in 2015")
print("Is it A:61,000 B:208,000 C:98,000 or D:18,000")
ans = str(input())
if ans == "D" or ans == "d" or ans == '100000' or ans == '4':
print("You got it right!")
right += 1
else:
print("You got it wrong!")
wrong += 1
question_number += 1
question_four()
questions()Digging deeper:
Globals
Try to avoid using globals as much as possible. Global variables should be avoided because they inhibit code reuse. The reason they are bad is that they allow functions to have hidden (as in "non-obvious" and "undeclared") and thus hard to understand side effects. Also, this can lead to Spaghetti code.
Duplicate code
As you can see, your functions are pretty much the same, so you can use this and try making a single method. More, we can get rid of that ugly usage of inner methods. It's just not necessary in this case.
What I'd do is:
- Create three lists: one containing questions, one answers and another one which contains the correct
Code Snippets
def questionOne():
....
def questionTwo():
....print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed",
questionNumber, "Questions\n")print()
print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions")
print()print("\nSo far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")Context
StackExchange Code Review Q#133906, answer score: 11
Revisions (0)
No revisions yet.