簡體   English   中英

如何為更高或更低的游戲增加分數

[英]how to add score to higher or lower game

from random import *

while True:

    random1 = randint(1,20)

    random2 = randint(1,20)

    print("h = higher, l = lower, s = same, q = quit")

    print(random1)

    a = input()

    if a.lower() == 'q':
            break

    print(random2)

    if a.lower() == 'h' and random1 < random2:

        print("Well done")

    elif a.lower() == 'l' and random1 > random2:

        print("Well done")

    elif a.lower() == 's' and random1 == random2:

        print("Well done")
    else:

        print("Loser")

所以我想做的是將x作為我的分數。 當答案打印“ Well Done”時,我希望它為我的分數加10分,然后打印分數。 問題是得分似乎在整個游戲中重置了時間負荷,我希望它可以將得分增加10或保持不變。 有誰知道在我的程序中執行此操作的方法。 我無法想象這太難了,但我只是一個初學者,還在學習。 目前,我的程序完全沒有評分,因此您可以向我展示最簡單/最佳的方法。 謝謝您的幫助 :)

x = 0 # initialize the score to zero before the main loop
while True:

    ...

    elif a.lower() == 's' and random1 == random2:
        x += 10 # increment the score
        print("Well done. Your current score is {0} points".format(x))

無論如何,整個代碼可以簡化為:

from random import *
x = 0
while True:
    random1 = randint(1,20)
    random2 = randint(1,20)
    print("h = higher, l = lower, s = same, q = quit")
    print(random1)
    a = input().lower()
    if a == 'q':
        break

    print(random2)

    if ((a == 'h' and random1 < random2) or
        (a == 'l' and random1 > random2) or
        (a == 's' and random1 == random2)):
        x += 10
        print("Well done. Your current score is: {0}".format(x))
    else:
        print("Loser")

只需添加一個變量:

score = 0 #Variable that keeps count of current score (outside of while loop)

while True:
...
    elif a.lower() == 'l' and random1 > random2:
        score += 10 #Add 10 to score
        print("Well done")
    else:
        #Do nothing with score as it stays the same
        print("Loser")

暫無
暫無

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

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