Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3.5.2中,是否可以删除整数的小数点,同时保留浮点数_Python - Fatal编程技术网

在Python 3.5.2中,是否可以删除整数的小数点,同时保留浮点数

在Python 3.5.2中,是否可以删除整数的小数点,同时保留浮点数,python,Python,如果我有一个整数和浮点数的列表,并且我打印它,有没有一种方法可以打印不带小数点的整数,但保留它用于浮点数 我根据牌组中的牌创建了一个游戏,不同的牌有不同的值。然后将每张卡的值乘以2、1.5、1或0.5(基于套装)。这是随机进行的。此过程重复10次 代码如下所示: Scores = [2, 4, 6, 8, 10, 12, 15] Suits = [2, 1.5, 1, 0.5] TScore = 0 for each in range(10): Score = random.choice(Sco

如果我有一个整数和浮点数的列表,并且我打印它,有没有一种方法可以打印不带小数点的整数,但保留它用于浮点数

我根据牌组中的牌创建了一个游戏,不同的牌有不同的值。然后将每张卡的值乘以2、1.5、1或0.5(基于套装)。这是随机进行的。此过程重复10次

代码如下所示:

Scores = [2, 4, 6, 8, 10, 12, 15]
Suits = [2, 1.5, 1, 0.5]
TScore = 0
for each in range(10):
Score = random.choice(Scores)
Suit = random.choice(Suits)
TScore = Score * Suit
print("Your score is {0}".format(TScore))
控制台可能会打印如下内容:

Scores = [2, 4, 6, 8, 10, 12, 15]
Suits = [2, 1.5, 1, 0.5]
TScore = 0
for each in range(10):
Score = random.choice(Scores)
Suit = random.choice(Suits)
TScore = Score * Suit
print("Your score is {0}".format(TScore))
你的分数是12分 你的分数是22.5分 你的分数是18.0分 你的分数是15分 你的分数是30分 你的分数是6.0分 你的分数是6.0分 你的分数是6.0分 你的分数是12分 你的分数是6分

有没有办法确保整数后面没有小数点?

g或n格式类型可以为您执行以下操作:

In [560]: for score in [4.0, 22.5]:
     ...:     print('Your score is {:n}'.format(score))
     ...:     
Your score is 4
Your score is 22.5
有关模或其他运算符的更多信息,请参见:

参考上述jonrsharpe的评论

    if score.is_integer(): # a float.method returns true if the number is whole.
        score = int(score)
    print(score)

有关float.methods的更多信息:

我希望它能在您的情况下起作用:

def removeDecimal(num):
        num1 = list(str(num))
        if(num1[-1]=='0'):      
                return int(num)
        else:
                return num

def main():
        score1 = 4.0
        score2 = 22.5
        print(removeDecimal(score1))    
        print(removeDecimal(score2))
if __name__=="__main__":
        main()
其输出为:

四,


22.5

您如何打印它们?你确定他们不是都是花车吗?一个也可以是整数的事实可以用float.is_integer测试;请参阅,例如,请提供更多详细信息和足够的数据,以便其他人提供帮助。这些都是浮动…@jornsharpe该问题已更新为code@adele已添加代码。if score.is_integer:如果数字为整数,则float.method返回true。score=intscore printscore如果这两种情况下的分数都是4.0或4.8,它会给出4。我很抱歉,我输入了一个语法错误,谢谢你指出它。