Reputation: 5
I am trying to use a square root as the count for a counted loop, but I keep getting the error "a float is required." I have tried every way that I can think of to convert the value provided by math.sqrt to a float, but I am still getting the message.
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()
Error:
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
Any help is greatly appreciated!
Upvotes: 0
Views: 1305
Reputation: 168626
You are missing a return
statement. Try this:
def getInfo():
tilesNum = float(input("Please enter number of tiles now: "))
return tilesNum
Since your getInfo()
has no explicit return statement, it returns None
implicitly. Your main()
then passes None
into procedure()
. Inside procedure()
, you pass None
into math.sqrt()
. math.sqrt()
complains with a TypeError
.
Upvotes: 4