-4

If I have a list of both integers and floats and I print it, is there a way to print the integers without a decimal place but keep it for the floats?

I created a game based on the cards in a deck where different cards have different values. The value of each card is then multiplied by either 2, 1.5, 1 or 0.5 based on the suit. This is done randomly. This process is repeated 10 times.

The code looks something like this:

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))

The console may print something like this:

Your score is 12
Your score is 22.5
Your score is 18.0
Your score is 15
Your score is 30
Your score is 6.0
Your score is 6.0
Your score is 6.0
Your score is 12
Your score is 6

Is there a way to make sure that the integers are not followed by a decimal point?

Mon
  • 19
  • 4
  • 1
    How are you printing them? Are you sure they're not *both* floats? The fact that one could also be an integer can be tested with `float.is_integer`; see e.g. http://stackoverflow.com/q/21583758/3001761 – jonrsharpe Dec 23 '16 at 19:49
  • 2
    Please provide more details and enough data to enable others to help. – nipy Dec 23 '16 at 19:54
  • Those are both floats... – juanpa.arrivillaga Dec 23 '16 at 20:14
  • @jonrsharpe the question has been updated with the code – Mon Dec 23 '16 at 20:50
  • @adele code has been added. – Mon Dec 23 '16 at 20:50
  • There is a chance some of your outputs will be of type `int`, while some will be integer values of type `float`, while others will be non-integer rational numbers of type `float`. I suggest you make _all_ the values in `Suits` into `floats` --- regardless of how you intend to display them later --- just to avoid the unexpected fun that mixing kind-of equivalent types can bring you. (Exhibit A: Some of your "integer" outputs have the trailing ".0" while some don't.) – Kevin J. Chase Dec 24 '16 at 04:21

3 Answers3

4

The g or n format type does this for you:

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

https://docs.python.org/3/library/string.html#format-specification-mini-language

hpaulj
  • 221,503
  • 14
  • 230
  • 353
1
for score in [4.0, 22.5]:
    '''
    Modulo(%) pulls the remainder, if the number is a whole number
    dividing by 1 should always return 0.
    '''
    if score % 1 == 0:
        score = int(score)
    print(score)

For more information on modulo or other operators: https://www.tutorialspoint.com/python/python_basic_operators.htm

In reference to jonrsharpe's comment above.

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

For more information on float.methods: https://docs.python.org/3/library/stdtypes.html#additional-methods-on-float

0

I hope it will work in your case:

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()

output for this is :

4

22.5

Aman Jaiswal
  • 1,084
  • 2
  • 18
  • 36