Ich versuche, eine Quadratwurzel als die Anzahl für eine gezählte Schleife zu verwenden, aber ich bekomme immer den Fehler "ein float ist erforderlich." Ich habe alles versucht, was ich mir vorstellen kann, um den von math.sqrt bereitgestellten Wert in ein Float zu konvertieren, aber ich bekomme immer noch die Nachricht.Python 3 - math.sqrt - ein Float ist erforderlich
Code:
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()
Fehler:
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
Jede Hilfe ist sehr zu schätzen!
- Rainy
Sie haben die richtige Antwort von @ Robᵩ. Wenn Sie das nächste Mal eine Nachricht wie diese erhalten, sollten Sie überprüfen, was tilesNum ist - platzieren Sie einen Haltepunkt in der Zeile und überprüfen Sie. Oder fügen Sie eine Druckanweisung hinzu. – zmbq