2014-03-05 82 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)