簡體   English   中英

Python游戲節目高分

[英]Python game show high score

我的游戲節目代碼得分很高,我寫的一切都正常,但是我無法得到它來打印最終得分,當我稱它為高分時,任何人都可以看一下在代碼上,告訴我我做錯了什么? 謝謝!

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.")

我建議您更改打印件。 你有這樣的事情:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM