分数显示之前拒绝重置为零

时间:2019-05-17 08:20:54

标签: python

我正在尝试用python创建骰子游戏,其中一项规则是,当分数低于零时,分数会重置。

我曾尝试将得分重置检查放在开始,结束和掷骰程序中,但它们始终允许其低于零。

#import all modules.
import math
import random 
print("successfully imported modules.")
roundno=0
p1score=0
p2score=0
p1dicecomb=0
p2dicecomb=0
p1dice1=0
p1dice2=0
p2dice1=0
p2dice2=0 # assign values.
while roundno < 6:
    if p2score < -1 or p2score == -1:
        p2score = 0
    if p1score < -1 or p1score == -1:
        p1score = 0
    print("round number is round", roundno) #new round always begins on player 1 turn.
    print("player 1")
    p1dice1 = random.randint(1,6)
    p1dice2 = random.randint(1,6)# roll dice using cpu.
    p1dicecomb = p1dice1+p1dice2
    if p1dicecomb % 2 == 0:
        p1score = p1score+10# compare the score to see if odd or even.
    if p1dicecomb % 2 == 1:
        p1score = p1score-5
    print ("score is", p1score)
    print("dice 1 is", p1dice1)
    print("dice 2 is", p1dice2)
    input("press enter to continue rolling dice.")
    print ("player 2")
    if p2score < -1 or p2score == -1:
        p2score = 0
    if p1score < -1 or p1score == -1:
        p1score = 0
    p2dice1 = random.randint(1,6)
    p2dice2 = random.randint(1,6)# roll dice using cpu.
    p2dicecomb = p2dice1+p2dice2

    if p2dicecomb % 2 == 0:
        p2score = p2score+10# compare the score to see if odd or even.
    if p2dicecomb % 2 == 1:
        p2score = p2score-5
    print ("score is", p2score)
    print("dice 1 is", p2dice1)
    print("dice 2 is", p2dice2)
    roundno = roundno+1
    input("press enter to continue rolling dice.")

我希望代码在达到-5时应将分数重置为零,但表示分数为负5。

4 个答案:

答案 0 :(得分:0)

设置分数后,您不会进行小于零的检查。

为了测试,我添加了以下几行:

#Stuff before...
    p2dice1 = random.randint(1,6)
    p2dice2 = random.randint(1,6)# roll dice using cpu.
    p2dicecomb = p2dice1+p2dice2

    print(p2score)
    print(p2dicecomb)

    if p2dicecomb % 2 == 0:
        p2score = p2score+10# compare the score to see if odd or even.
    if p2dicecomb % 2 == 1:
        p2score = p2score-5
    print ("score is", p2score)

您将看到第一次打印p2score时它为0。如果p2dicecomb为奇数,则p2score变为0-5,即-5!

稍微重新排列代码以设置减去后的检查 ,例如:

import math
import random 
print("successfully imported modules.")
roundno=0
p1score=0
p2score=0
p1dicecomb=0
p2dicecomb=0
p1dice1=0
p1dice2=0
p2dice1=0
p2dice2=0 # assign values.
while roundno < 6:
    if p2score < -1 or p2score == -1:
        p2score = 0
    if p1score < -1 or p1score == -1:
        p1score = 0
    print("round number is round", roundno) #new round always begins on player 1 turn.
    print("player 1")
    p1dice1 = random.randint(1,6)
    p1dice2 = random.randint(1,6)# roll dice using cpu.
    p1dicecomb = p1dice1+p1dice2
    if p1dicecomb % 2 == 0:
        p1score = p1score+10# compare the score to see if odd or even.
    if p1dicecomb % 2 == 1:
        p1score = p1score-5
    if p1score < -1 or p1score == -1:
        p1score = 0
    print ("score is", p1score)
    print("dice 1 is", p1dice1)
    print("dice 2 is", p1dice2)
    input("press enter to continue rolling dice.")
    print ("player 2")

    p2dice1 = random.randint(1,6)
    p2dice2 = random.randint(1,6)# roll dice using cpu.
    p2dicecomb = p2dice1+p2dice2

    if p2dicecomb % 2 == 0:
        p2score = p2score+10# compare the score to see if odd or even.
    if p2dicecomb % 2 == 1:
        p2score = p2score-5

    if p2score < -1 or p2score == -1:
        p2score = 0
    print ("score is", p2score)
    print("dice 1 is", p2dice1)
    print("dice 2 is", p2dice2)
    roundno = roundno+1
    input("press enter to continue rolling dice.")

(您也可以导入math但不要使用它!)

答案 1 :(得分:0)

您有两个非常相似的代码块:

if p1dicecomb % 2 == 1:
    p1score = p1score-5
print ("score is", p1score)

然后

if p2dicecomb % 2 == 1:
    p2score = p2score-5
print ("score is", p2score)

减去5与打印件之间没有任何关系,因此,如果数字原为0,则可以减为5。

您确实有一段代码将值设为零:

if p2score < -1 or p2score == -1:
    p2score = 0
if p1score < -1 or p1score == -1:
    p1score = 0

但是在减去5之前会发生这种情况。 这些行是按顺序执行的,因此您需要将其移动到减去后的值

值得学习如何调试代码,因此您可以一次浏览一行,看看会发生什么。

现在,一些样式要点。您可以将重复的部分拉出小功能。或考虑游戏中的步骤。

您必须更新分数,但要确保update不会使分数降至零以下。

您可以执行以下操作:

  def update (dicecomb, score):
    if dicecomb % 2 == 0:
        score = score+10# compare the score to see if odd or even.
    if dicecomb % 2 == 1:
        score = score-5
    return max(score, 0)

并在主循环中调用它。 max是一个标准函数,它将选择较大的数字。如果0较大,则不会变为负数。

更简单地说,您可以删除所有对负数的检查,然后像这样更新:

if p2dicecomb % 2 == 0:
    p2score = p2score+10# compare the score to see if odd or even.
if p2dicecomb % 2 == 1:
    p2score = max(p2score-5, 0)

答案 2 :(得分:0)

这是操作顺序的问题:

  1. 如果得分为负,则将得分重置为0
  2. 你掷骰子
  3. 您应用+10或-5操作
  4. 您打印结果,此结果可能为负。

因此,有时候打印的分数是负数是正常的……

这是一个有效的示例:

import random 

roundno = 0
p1score, p2score = 0, 0
p1dice1, p1dice2 = 0, 0
p2dice1, p2dice2 = 0, 0

while roundno < 6:
    print("round number is round", roundno) #new round always begins on player 1 turn.

    print("player 1")
    p1dice1, p1dice2 = random.randint(1,6), random.randint(1,6) # roll dice using cpu.

    # compare the score to see if odd or even.
    if (p1dice1 + p1dice2) % 2 == 0:
        p1score += 10
    else:
        p1score -= 5

    if p1score < 0:
        p1score = 0

    print ("score is", p1score)
    print("dice 1 is", p1dice1)
    print("dice 2 is", p1dice2)

    input("press enter to continue rolling dice.")

    print ("player 2")

    # roll dice using cpu.
    p2dice1, p2dice2 = random.randint(1,6), random.randint(1,6)

    # compare the score to see if odd or even.
    if (p2dice1 + p2dice2) % 2 == 0:
        p2score += 10   
    else:
        p2score -= 5

    if p2score < 0:
        p2score = 0

    print ("score is", p2score)
    print("dice 1 is", p2dice1)
    print("dice 2 is", p2dice2)

    roundno = roundno+1
    input("press enter to continue rolling dice.")

答案 3 :(得分:0)

重新阅读我的代码后,我发现代码不正确。

正确的代码是:

    p1dicecomb = p1dice1+p1dice2
    if p1dicecomb % 2 == 0:
        p1score = p1dicecomb +10# compare the score to see if odd or even.
    if p1dicecomb % 2 == 1:
        p1score = p1dicecomb-5

这为我解决了这个问题。