我有一个python脚本,在运行时会将数字2048
除以2
所有数字到129
的数字范围(但不包括)和将打印它等于的东西。因此,我们知道有些数字会均匀地进入2048
,但有些则没有,所以我的问题是如何制作它以便我的脚本只打印出整数。
我已经能够弄明白但我觉得这不是最好的方法,它有一些缺点。如果你愿意,我可以将代码放入问题,但就像我说的那样,我觉得这不是最合乎逻辑的做法。
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")
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
预期输出:我希望可以省略不可转换为整数的数字。
答案 0 :(得分:7)
如果user_input / num
给出剩余的0
,user_input
可以被num
整除。您可以使用%
(模数)运算符来检查:
if user_input % num == 0:
print("{} / {} = {}".format(user_input, num, user_input // num))
另外,使用input()
将输入作为字符串输入:
user_input = input("Enter a number:")
并且,使用try
尝试将其转换为整数,并sys.exit
退出脚本,如果不是:
try:
user_input = int(user_input)
except ValueError:
print("Please input a whole number")
import sys
sys.exit(1)
答案 1 :(得分:3)
我希望可以省略不可转换为整数的数字。
在这种情况下,请使用额外的if语句,并在检查前不要将x
转换为字符串:
if x.is_integer():
print(y, "/", num, "=", str(x))
演示:
>>> x = 3.14
>>> x.is_integer()
False
>>> x = 3.0
>>> x.is_integer()
True
答案 2 :(得分:0)
如果要截断数字,可以将它们转换为int。它是使用int(数字)函数完成的。
Dim returnVal As Variant
returnVal = InputBox(Prompt:="Type a value:", Title:="Test Data")
'if the user clicked Cancel, Close or Esc the False
'is translated to the variant as a vbNullString
Select Case True
Case Len(returnVal) = 0
'no value but user clicked OK - clear the target cell
Range("A2").ClearContents
Case Else
'returned a value with OK, save it
Range("A2") = returnVal
End Select
答案 3 :(得分:0)
字符串format
可以为您执行此操作,也会自动为您舍入。
print(y, "/", num, "=", "{:.0f}".format(x))