【发布时间】:2021-11-19 11:28:27
【问题描述】:
到目前为止,我已经尝试创建将打印和计算所有类别百分比的 QBank 类,以及将输出个性类型的 PType 类。这是非面向对象的代码:
apercent = {}
apercent["A"] = 0
apercent["B"] = 0
apercent["C"] = 0
apercent["D"] = 0
questions = [
{"q": "Would you call yourself a thinker", "f": "D"},
{"q": "Are you creative", "f": "D"},
{"q": "Do you like philosophy", "f": "D"},
{"q": "Do you like to think about complex questions", "f": "D"},
{"q": "Do you have an interest in artisitc pursuits like painting and writing", "f": "D"},
{"q": "Do you like initiating conversations", "f": "A"},
{"q": "Do you like to start talks with new people", "f": "A"},
{"q": "Is it easy for you to adjust in an enviroment where you initially do not know anyone", "f": "A"},
{"q": "Are you social", "f": "A"},
{"q": "Do you like social events (parties, fests and events)", "f": "A"},
{"q": "Do you mostly make your decisions by heart", "f": "B"},
{"q": "Do you put others before yourself", "f": "B"},
{"q": "Do you think that one should change their views if they hurt someone", "f": "B"},
{"q": "Do you often get lost in your feelings", "f": "B"},
{"q": "Are you run more by your feelings than logic", "f": "B"},
{"q": "Can you make a timetable and stick to it", "f": "C"},
{"q": "Do you usually follow the rules/laws", "f": "C"},
{"q": "Are you organised", "f": "C"},
{"q": "Do you plan before doing something", "f": "C"},
{"q": "Are you ok with following orders", "f": "C"}
]
def question(index, inp, func):
if bool(input(str(index + 1) + ". " + inp + "? Yes/No:\t").lower().strip("no. \t")):
apercent[func] = apercent[func] + 20
try:
for q in range(len(questions)):
question(q, questions[q]["q"], questions[q]["f"])
except KeyboardInterrupt:
exit(0)
print("Your extroversion percentage : ", apercent["A"], "%")
print("Your creativity percentage : ", apercent["N"], "%")
print("Your feeling percentage : ", apercent["B"], "%")
print("Your conscientious percentage : ", apercent["C"], "%")
if apercent["N"] > 50:
sn = "N"
else:
sn = "S"
if apercent["A"] > 50:
ie = "E"
else:
ie = "I"
if apercent["C"] > 50:
jp = "J"
else:
jp = "P"
if apercent["B"] > 50:
tf = "F"
else:
tf = "T"
ptype = ie + sn + tf + jp
print("Your personality type is: ", ptype)
这就是我尝试使 QBank 面向对象的方式:
class QBank:
def _init_(self, questions, percentage):
self.questions = questions
self.percentage = percentage
def printQuestions(self, q):
q = ["Would you call yourself a thinker (Y/N):",
"Are you creative (Y/N):",
"Do you like philosophy (Y/N):",
"Do you like to think about complex questions (Y/N):",
"Do you have an interest in artisitc pursuits like painting and writing (Y/N):",
"Do you like initiating conversations (Y/N):",
"Do you like to start talks with new people (Y/N):",
"Is it easy for you to adjust in an enviroment where you initially do not know anyone (Y/N):",
"Are you social (Y/N):",
"Do you like social events (parties, fests and events) (Y/N):",
"Do you mostly make your decisions by heart (Y/N):",
"Do you put others before yourself (Y/N):",
"Do you think that one should change their views if they hurt someone (Y/N):",
"Do you often get lost in your feelings (Y/N):",
"Are you run more by your feelings than logic (Y/N):",
"Can you make a timetable and stick to it (Y/N):",
"Do you usually follow the rules/laws (Y/N):",
"Are you organised (Y/N):",
"Do you plan before doing something (Y/N):",
"Are you ok with following orders (Y/N):"]
f= ["D","D","D","D","D","A","A","A","A","A","B","B","B","B","B","C","C","C","C","C",]
z = list(zip(q,f))
for x in range (len(q)):
print(q, input(" "))
print(QBank.printQuestions(q))
这是针对 PType 的
class PType:
def __init__(self, personality, anime, movie, career):
self.personality = personality
self.anime = anime
self.movie = movie
self.career = career
def printValues(self):
print("Your personality type is: ", self.personality)
print("Anime character: ", self.anime)
print("Tv show/movie character: ", self.movie)
print("Career options: ", self.career)
def calculatePtype(self):
pass
p1 = PType("ENTJ", "Mikasa Ackerman", "Harrison Wells", "Businessman")
p1.printValues()
我只是真的坚持如何在面向对象的 python 编程中做到这一点。特别是对于计算。有人可以帮我吗?
【问题讨论】:
标签: python object-oriented-analysis