2014-03-05 51 views
0

我遇到了我的游戏节目代码的高分问题,我写了一切正常,但我无法得到它打印最终的分数,它不会打印出高分我称之为任何人都可以看看代码并告诉我我做错了什么?谢谢!Python游戏节目高分

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

什么是电流输出? – Raptor

回答

1

我建议您更改打印件。你有这样的事情:

print("Your score is: %d/" % correct + str(num_ques)) 

你正在使用2种方式的连接。 %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' 

为了您的代码,我看到你存储玩家在变量中的得分“正确”,因此要显示“您的得分是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)