使用Python创建自己的性格测验
确保Visual Studio Code准备好创建性格测验
创建一个文件夹Sleuth,然后在Visual Studio Code里创建一个文件名quiz.py。
超级侦探性格测验需要的Python基础知识
注释:
在使用 # 符号的任何行上,Python 都会忽略该符号后的所有内容。
变量:
条件语句:
Python中的条件:
布尔(附加知识):
在布尔值有true和falsh。
在超级侦探性格测验中向用户提问
要求测验者输入
发现了Visual studio Code的缺陷。
print( "Hello, Contosoville!" )
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
# print out which activity they chose
print( f"You chose {activity}.")
# if they chose reading a book
if activity == "A":
print( "Nice choice!" )
if activity == "B":
print( "Sounds fun!" )
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
Hello, Contosoville!
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
a
You chose a.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
Hello, Contosoville!
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
A
You chose A.
Nice choice!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
Hello, Contosoville!
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
B
You chose B.
Sounds fun!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>
在超级侦探性格测验中检查用户的答复
多个if语句
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
# print out which activity they chose
print( f"You chose {activity}.")
if activity == "A":
print( "Nice choice!" )
if activity == "B":
print( "Sounds fun!" )
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
A
You chose A.
Nice choice!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>
else语句
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
# print out which activity they chose
print( f"You chose {activity}.")
if activity == "A":
print( "Nice choice!" )
else:
print( "Sounds fun!" )
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
A
You chose A.
Nice choice!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>
elif语句
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
# print out which activity they chose
print( f"You chose {activity}.")
if activity == "A":
print( "Nice choice!" )
elif activity == "B":
print( "Sounds fun!" )
else:
print("You must type A or B, let's just say you like to read.")
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
a
You chose a.
You must type A or B, let's just say you like to read.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>
完成超级侦探性格测验
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
if activity == "A":
print( "Reading a book, nice choice!" )
elif activity == "B":
print( "Attending a party? Sounds fun!" )
else:
print("You must type A or B, let's just say you like to read.")
activity = "A"
# ask the candidate a second question
job = input( "What's your dream job?\n(A) Curator at the Smithsonian\n(B) Running a business\n" )
if job == "A":
print( "Curator, nice choice!" )
elif job =="B":
print( "Running a business? Sounds fun!" )
else:
print("You must type A or B, let's just say you want to be a curator at the Smithsonian")
job = "A"
# ask the candidate a third question
value = input( "What's more important?\n(A) Money\n(B) Love\n" )
if value == "A":
print( "Money, nice choice!" )
elif value =="B":
print( "Love? Sounds fun!" )
else:
print("You must type A or B, let's just say money is more important to you.")
value = "A"
# ask the candidate a fourth question
decade = input( "What's your favorite decade?\n(A) 1910s\n(B) 2010s\n" )
if decade == "A":
print( "1910s, nice choice!" )
elif decade =="B":
print( "2010s? Sounds fun!" )
else:
print("You must type A or B, let's just say the 1910s is your favorite decade.")
decade = "A"
# ask the candidate a fifth question
travel = input( "What's your favorite way to travel?\n(A) Driving\n(B) Flying\n" )
if travel == "A":
print( "Driving, nice choice!" )
elif travel =="B":
print( "Flying? Sounds fun!" )
else:
print("You must type A or B, let's just say your favorite way to travel is by driving")
travel = "A"
# print out their choices
print( f"You chose {activity}, then {job}, then {value}, then {decade}, then {travel}.")
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
A
Reading a book, nice choice!
What's your dream job?
(A) Curator at the Smithsonian
(B) Running a business
B
Running a business? Sounds fun!
What's more important?
(A) Money
(B) Love
B
Love? Sounds fun!
What's your favorite decade?
(A) 1910s
(B) 2010s
B
2010s? Sounds fun!
What's your favorite way to travel?
(A) Driving
(B) Flying
A
Driving, nice choice!
You chose A, then B, then B, then B, then A.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>
根据性格测验分数选择超级侦探角色
# ask the candidate a question
activity = input( "How would you like to spend your evening?\n(A) Reading a book\n(B) Attending a party\n" )
if activity == "A":
print( "Reading a book, nice choice!" )
elif activity == "B":
print( "Attending a party? Sounds fun!" )
else:
print("You must type A or B, let's just say you like to read.")
activity = "A"
# ask the candidate a second question
job = input( "What's your dream job?\n(A) Curator at the Smithsonian\n(B) Running a business\n" )
if job == "A":
print( "Curator, nice choice!" )
elif job =="B":
print( "Running a business? Sounds fun!" )
else:
print("You must type A or B, let's just say you want to be a curator at the Smithsonian")
job = "A"
# ask the candidate a third question
value = input( "What's more important?\n(A) Money\n(B) Love\n" )
if value == "A":
print( "Money, nice choice!" )
elif value =="B":
print( "Love? Sounds fun!" )
else:
print("You must type A or B, let's just say money is more important to you.")
value = "A"
# ask the candidate a fourth question
decade = input( "What's your favorite decade?\n(A) 1910s\n(B) 2010s\n" )
if decade == "A":
print( "1910s, nice choice!" )
elif decade =="B":
print( "2010s? Sounds fun!" )
else:
print("You must type A or B, let's just say the 1910s is your favorite decade.")
decade = "A"
# ask the candidate a fifth question
travel = input( "What's your favorite way to travel?\n(A) Driving\n(B) Flying\n" )
if travel == "A":
print( "Driving, nice choice!" )
elif travel =="B":
print( "Flying? Sounds fun!" )
else:
print("You must type A or B, let's just say your favorite way to travel is by driving")
travel = "A"
# print out their choices
print( f"You chose {activity}, then {job}, then {value}, then {decade}, then {travel}.")
# create some variables for scoring
sam_like = 0
cam_like = 0
kai_like = 0
indy_like = 0
# update scoring variables based on the activity choice
if activity == "A":
sam_like = sam_like + 2
indy_like = indy_like + 2
kai_like = kai_like + 2
else:
cam_like = cam_like + 1
indy_like = indy_like + 1
# update scoring variables based on the job choice
if job == "A":
sam_like = sam_like + 2
indy_like = indy_like + 2
cam_like = cam_like - 1
else:
sam_like = sam_like - 1
kai_like = kai_like + 2
indy_like = indy_like + 1
# update scoring variables based on the value choice
if value == "A":
sam_like = sam_like - 1
kai_like = kai_like + 1
else:
sam_like = sam_like + 2
cam_like = cam_like + 2
indy_like = indy_like + 1
# update scoring variables based on the decade choice
if decade == "A":
cam_like = cam_like + 2
sam_like = sam_like + 2
else:
kai_like = kai_like + 1
indy_like = indy_like + 2
# update scoring variables based on the travel choice
if travel == "A":
sam_like = sam_like - 2
kai_like = kai_like + 1
indy_like = indy_like - 1
else:
sam_like = sam_like + 1
cam_like = cam_like + 1
kai_like = kai_like - 1
# print the results depending on the score
if sam_like >= 3:
print( "You're most like Sharp-Eyed Sam!" )
elif cam_like >= 3:
print( "You're most like Curious Cam!" )
elif kai_like >= 3:
print( "You're most like Keen Kai!" )
else:
print( "You're most like Inquisitive Indy!" )
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth> python quiz.py
How would you like to spend your evening?
(A) Reading a book
(B) Attending a party
B
Attending a party? Sounds fun!
What's your dream job?
(A) Curator at the Smithsonian
(B) Running a business
B
Running a business? Sounds fun!
What's more important?
(A) Money
(B) Love
A
Money, nice choice!
What's your favorite decade?
(A) 1910s
(B) 2010s
A
1910s, nice choice!
What's your favorite way to travel?
(A) Driving
(B) Flying
B
Flying? Sounds fun!
You chose B, then B, then A, then A, then B.
You're most like Curious Cam!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\Sleuth>