python - 如果数字低于零,如何将数字设置为零?
问题描述
我正在为掷骰子游戏制作代码。我想让玩家的分数永远不会低于零。有任何想法吗?
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)
解决方案
因为d_2
是一个整数而不是一个列表,而不是这样做(这会出错,因为你不能附加到一个整数);
if d_2 <= 0:
d_2.append(0)
你应该有这样的;
if d_2 < 0:
d_2 = 0
推荐阅读
- terraform - 如何通过openstack中的terraform将多个卷动态附加到多个实例?
- pandas - 如何计算列表元素并制作表格?
- c++ - 图像 OpenCV C++ 中的对数失真
- python - 为视频分类调整训练数据时出错
- java - 有没有更好的方法来为操纵杆输入做很多 if else 语句
- c# - EWS 连接给出未经授权的 (401) 错误
- javascript - 选择下拉菜单未更新散景图(使用 CustomJS)
- git - Git 分支高效工作流程
- c++ - 无法让 QSettings 正确写入我的设置
- react-native - 如何在本机反应中使用来自axios post的数组