首页 > 解决方案 > 如果数字低于零,如何将数字设置为零?

问题描述

我正在为掷骰子游戏制作代码。我想让玩家的分数永远不会低于零。有任何想法吗?

from random import choice
print("Lets Play")
play1 = input("Player 1 name: ")
play2 = input("Player 2 name: ")
print("Hi " + play1 + " & " + play2 + ", let" + "'" + "s roll the dice")

die = list(range(2, 13))

d_1 = choice(die)
print(play1, "Your number is...\n{}".format(d_1))

d_2 = choice(die)
print(play2, "Your number is...\n{}".format(d_2))

if not d_1 % 2:
    d_1 += 10
else:
    d_1 -= 5

if not d_2 % 2:
    d_2 += 10
else:
    d_2 -= 5
if d_1 <= 0:
    print ("test")

if d_2 <= 0:
    d_2.append(0)
print (play1, "Your total points is",d_1)
print (play2, "Your total points is",d_2)

标签: python

解决方案


因为d_2是一个整数而不是一个列表,而不是这样做(这会出错,因为你不能附加到一个整数);

if d_2 <= 0:
    d_2.append(0)

你应该有这样的;

if d_2 < 0:
    d_2 = 0

推荐阅读