Sayılan bir döngü için bir kare kökü kullanmaya çalışıyorum, ancak "float gerekli" hatasını almaya devam ediyorum. Math.sqrt tarafından sağlanan değeri bir float'a dönüştürmeyi düşünebilmem için her yolu denedim, ancak yine de mesajı alıyorum.python 3 - math.sqrt - bir float gerekli
Kodu:
from random import randrange
import math
def getInfo():
tilesNum = float(input("Please enter number of tiles now: "))
def procedure(tilesNum):
countX = 0
SqRoot = float(math.sqrt(tilesNum))
print(SqRoot)
for i in range(float(SqRoot)):
countX = countX + 1
countY = 0
for i in range(float(SqRoot)):
countY = countY + 1
terrain = randrange(1, 5)
if terrain == 1:
print("terrain on tile", countX, countY, "is frozen")
elif terrain == 2:
print("terrain on tile", countX, countY, "is flooded")
elif terrain == 3:
print("terrain on tile", countX, countY, "is impeded by rocks")
else:
print("terrain on tile", countX, countY, "is overgrown")
def main():
tilesNum = getInfo()
procedure(tilesNum)
main()
Hata:
Traceback (most recent call last):
File "C:\Users\Rainy\AppData\Local\Programs\Python\Python35\PBA prototype (problem).py", line 50, in <module>
main()
File "C:\Users\Rainy\AppData\Local\Programs\Python\Python35\PBA prototype (problem).py", line 48, in main
procedure(tilesNum)
File "C:\Users\Rainy\AppData\Local\Programs\Python\Python35\PBA prototype (problem).py", line 15, in procedure
SqRoot = float(math.sqrt(tilesNum))
TypeError: a float is required
Herhangi bir yardım büyük beğeni topluyor!
- Yağmurlu
Doğru yanıtı @ Robᵩ'den aldınız. Bir dahaki sefere böyle bir mesaj alırsanız, hangi tileNum olduğunu kontrol etmelisiniz - çizgide bir kesme noktası yerleştirin ve kontrol edin. Veya bir baskı ifadesi ekleyin. – zmbq