Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I only print whole numbers in python script

I have a python script that when run will take the number 2048 and divide it by a range of numbers from 2 all of the up to 129 (but not including) and will print what it equals. So as we know some numbers go into 2048 evenly but some do not so my question here is how can I make it so my script only prints out whole numbers.

I have been able to figure it out but I felt like it was not the best way of doing it and it had some drawbacks. If you want to I can put that code into the question but like I said I did not feel like it was the most logical way of doing it.

Script.py

user_input = 2048
user_input = str(user_input)

if user_input.isdigit():
    user_input = int(user_input)
    for num in range(2, 129):
        y = user_input
        x = user_input / num
        x = str(x)
    print(y, "/", num, "=", x)
else:
    print("Please enter a whole number")

Output of Script.py

2048 / 2 = 1024.0
2048 / 3 = 682.6666666666666
2048 / 4 = 512.0
2048 / 5 = 409.6
2048 / 6 = 341.3333333333333
2048 / 7 = 292.57142857142856
2048 / 8 = 256.0
2048 / 9 = 227.55555555555554
2048 / 10 = 204.8
2048 / 11 = 186.1818181818182
2048 / 12 = 170.66666666666666
2048 / 13 = 157.53846153846155
2048 / 14 = 146.28571428571428
2048 / 15 = 136.53333333333333
2048 / 16 = 128.0
2048 / 17 = 120.47058823529412
2048 / 18 = 113.77777777777777
2048 / 19 = 107.78947368421052
2048 / 20 = 102.4
2048 / 21 = 97.52380952380952
2048 / 22 = 93.0909090909091
2048 / 23 = 89.04347826086956
2048 / 24 = 85.33333333333333
2048 / 25 = 81.92
2048 / 26 = 78.76923076923077
2048 / 27 = 75.85185185185185
2048 / 28 = 73.14285714285714
2048 / 29 = 70.62068965517241
2048 / 30 = 68.26666666666667
2048 / 31 = 66.06451612903226
2048 / 32 = 64.0
2048 / 33 = 62.06060606060606
2048 / 34 = 60.23529411764706
2048 / 35 = 58.51428571428571
2048 / 36 = 56.888888888888886
2048 / 37 = 55.351351351351354
2048 / 38 = 53.89473684210526
2048 / 39 = 52.51282051282051
2048 / 40 = 51.2
2048 / 41 = 49.951219512195124
2048 / 42 = 48.76190476190476
2048 / 43 = 47.627906976744185
2048 / 44 = 46.54545454545455
2048 / 45 = 45.51111111111111
2048 / 46 = 44.52173913043478
2048 / 47 = 43.57446808510638
2048 / 48 = 42.666666666666664
2048 / 49 = 41.795918367346935
2048 / 50 = 40.96
2048 / 51 = 40.15686274509804
2048 / 52 = 39.38461538461539
2048 / 53 = 38.64150943396226
2048 / 54 = 37.925925925925924
2048 / 55 = 37.236363636363635
2048 / 56 = 36.57142857142857
2048 / 57 = 35.92982456140351
2048 / 58 = 35.310344827586206
2048 / 59 = 34.71186440677966
2048 / 60 = 34.13333333333333
2048 / 61 = 33.57377049180328
2048 / 62 = 33.03225806451613
2048 / 63 = 32.507936507936506
2048 / 64 = 32.0
2048 / 65 = 31.50769230769231
2048 / 66 = 31.03030303030303
2048 / 67 = 30.567164179104477
2048 / 68 = 30.11764705882353
2048 / 69 = 29.681159420289855
2048 / 70 = 29.257142857142856
2048 / 71 = 28.845070422535212
2048 / 72 = 28.444444444444443
2048 / 73 = 28.054794520547944
2048 / 74 = 27.675675675675677
2048 / 75 = 27.30666666666667
2048 / 76 = 26.94736842105263
2048 / 77 = 26.5974025974026
2048 / 78 = 26.256410256410255
2048 / 79 = 25.924050632911392
2048 / 80 = 25.6
2048 / 81 = 25.28395061728395
2048 / 82 = 24.975609756097562
2048 / 83 = 24.674698795180724
2048 / 84 = 24.38095238095238
2048 / 85 = 24.094117647058823
2048 / 86 = 23.813953488372093
2048 / 87 = 23.54022988505747
2048 / 88 = 23.272727272727273
2048 / 89 = 23.01123595505618
2048 / 90 = 22.755555555555556
2048 / 91 = 22.505494505494507
2048 / 92 = 22.26086956521739
2048 / 93 = 22.021505376344088
2048 / 94 = 21.78723404255319
2048 / 95 = 21.557894736842105
2048 / 96 = 21.333333333333332
2048 / 97 = 21.11340206185567
2048 / 98 = 20.897959183673468
2048 / 99 = 20.68686868686869
2048 / 100 = 20.48
2048 / 101 = 20.277227722772277
2048 / 102 = 20.07843137254902
2048 / 103 = 19.883495145631066
2048 / 104 = 19.692307692307693
2048 / 105 = 19.504761904761907
2048 / 106 = 19.32075471698113
2048 / 107 = 19.14018691588785
2048 / 108 = 18.962962962962962
2048 / 109 = 18.788990825688074
2048 / 110 = 18.618181818181817
2048 / 111 = 18.45045045045045
2048 / 112 = 18.285714285714285
2048 / 113 = 18.123893805309734
2048 / 114 = 17.964912280701753
2048 / 115 = 17.808695652173913
2048 / 116 = 17.655172413793103
2048 / 117 = 17.504273504273506
2048 / 118 = 17.35593220338983
2048 / 119 = 17.210084033613445
2048 / 120 = 17.066666666666666
2048 / 121 = 16.925619834710744
2048 / 122 = 16.78688524590164
2048 / 123 = 16.650406504065042
2048 / 124 = 16.516129032258064
2048 / 125 = 16.384
2048 / 126 = 16.253968253968253
2048 / 127 = 16.125984251968504
2048 / 128 = 16.0

Expected output: I would like the numbers that are not convertible to integers to just be omited.

like image 811
Alex Lowe Avatar asked Jul 18 '26 05:07

Alex Lowe


1 Answers

If user_input / num gives a remainder of 0, that user_input is evenly divisible by num. You can check this with the % (modulo) operator:

if user_input % num == 0:
    print("{} / {} = {}".format(user_input, num, user_input // num))

Also, use input() to get the input as a string:

user_input = input("Enter a number:")

And, use try to try converting it to an integer, and sys.exit to exit the script if it isn't:

try:
    user_input = int(user_input)
except ValueError:
    print("Please input a whole number")
    import sys
    sys.exit(1)
like image 82
Claudiu Avatar answered Jul 20 '26 17:07

Claudiu