我遇到了我的游戏节目代码的高分,我写的所有内容都有效,但我无法打印出最终分数,当我打电话给任何人时都不会打印出高分看看代码并告诉我我做错了什么?谢谢!
num_ques = 0
correct = 0
for question_object in questions:
print(question_object["question"])
for i, choice in enumerate(question_object["answers"]):
print(str(i + 1) + ". " + choice)
answer = input("Choose an answer 1-4:")
num_ques = num_ques + 1
if answer == question_object["correct"]:
print("Bravo. You're a nerd")
correct = correct + 1
print("Your score is: %d/" % correct + str(num_ques))
else:
print("Your score is: %d/" % correct + str(num_ques))
print("Well at least you have a life.")
答案 0 :(得分:1)
我建议您更改打印件。你有这样的事情:
print("Your score is: %d/" % correct + str(num_ques))
您正在使用两种连接方式。 %d和'+'。您可以使用以下连接:
a='Hello'
b='World'
print a+b #This would print 'HelloWorld'
但你也可以
print '%s%s' % (a,b) #This would print 'HelloWorld' too
您可以使用以下格式连接不同的类型:
a='I have'
b=1
c='year old.'
print '%s %d %s' % (a,b,c) #This would print 'I have 1 year old'
对于你的代码我看到你将玩家的分数存储在变量“correct”中,所以为了显示“你的分数是7”,“7”在'正确'里面,它是一个整数。 (如果要连接的变量是一个整数,则使用%d,如果是一个字符串,则使用%s)
print "Your score is: %d" % (correct)
如果你有多个变量,比如“你的分数是X / Y”,假设X是正确的答案,而Y则回答了总问题:
print "Your score is %d/%d" % (correct, num_ques)
你可以根据需要连接多个变量,%d和%s的顺序是括号之间变量的顺序
要显示包含最终得分的消息,您可以在for结束时添加打印内容,如下所示:
print "Your final score is: %d!!!!!" % (correct)
为此,您的代码将是:
num_ques = 0
correct = 0
for question_object in questions:
print(question_object["question"])
for i, choice in enumerate(question_object["answers"]):
print(str(i + 1) + ". " + choice)
answer = input("Choose an answer 1-4:")
num_ques = num_ques + 1
if answer == question_object["correct"]:
print "Bravo. You're a nerd"
correct = correct + 1
print "Your score is: %d/%d" % (correct, num_ques)
else:
print "Your score is: %d/%d" % (correct, num_ques)
print "Well at least you have a life."
print "Your final score is: %d/%d!!!!!" % (correct, num_quest)