TypeError: unsupported operand type(s) for /: 'str' and 'float'
I'm making a football game, and I get this whenever I try to run the following code to determine how far a play will go.
playdistance = round(random.uniform(float(rbs.get(possession)[-2:]/float(30.0))-2.5,float(rbs.get(possession)[-2:]/float(30.0)) 5.5))
"rbs" is a dictionary containing all of the teams' running backs and overall stored like 'NYG':'Saquon Barkley 99'
where it contains the name and then how good that player is on a scale from 0-99. I stored it like this so that I can use [-2:] to get how good the player is, and [:-2] to get the name of the player.
"possession" is the team that has the ball, so that I can pull the running back's name and skill from the dictionary previously mentioned.
What I'm confused about is how I'm getting the previously mentioned error when both of the arguments in the division are floats, and neither are strings. I've tried converting the 30 divisor into a string, a float, and I've also done that for the first argument.
I'm sure this is a pretty dumb question as I am pretty new to coding and python, but if someone could help me out that would be awesome.
CodePudding user response:
When I indent your code to make it more readable, the problem becomes evident
playdistance = round(
random.uniform(
float(
rbs.get(possession)[-2:] / float(30.0) # error 1
) - 2.5, float(
rbs.get(possession)[-2:] / float(30.0) # error 2
) 5.5
)
)
rbs.get(possession)[-2:]
is a string. I see you're trying to convert it to a float, but that needs to be done with
float(rbs.get(possession)[-2:]) / float(30.0) - 2.5
# ^
not
float(rbs.get(possession)[-2:] / float(30.0)) - 2.5
# ^
There's no need to parenthesize the division, because /
has higher precedence than -
(remember your order of operations)
CodePudding user response:
You can't combine strings and numbers like that in Python.
rbs.get(possession)[-2:]
gives you a string, e.g. '99'
, and float(30.0)
gives you a number. The division of strings by numbers is not defined.
You must convert the '99' to a number first before you can divide it by anything. Technically speaking, you only need to switch the parentheses around in your expression.
Broken:
round(random.uniform(float(rbs.get(possession)[-2:]/float(30.0))-2.5,float(rbs.get(possession)[-2:]/float(30.0)) 5.5))
Working:
round(random.uniform(float(rbs.get(possession)[-2:])/float(30.0)-2.5,float(rbs.get(possession)[-2:])/float(30.0) 5.5))
but practically speaking, use variables. The stuff above is all but unreadable.
player_rating = float(rbs.get(possession)[-2:])
low = player_rating / 30 - 2.5
high = player_rating / 30 5.5
playdistance = round(random.uniform(low, high))
Also, once a variable in a calculation is a float, such as player_rating
here, the entire calculation will yield a float. Things like float(30.0)
are completely unnecessary.
CodePudding user response:
That fixes your problem where you need to convert to float
before your divided
import numpy
rbs={'NYG':'Saquon Barkley 99'
}
playdistance = float(round(numpy.random.uniform(float(rbs.get(possession)[-2:])/float(30.0)-2.5,float(rbs.get(possession)[-2:])/float(30.0)) 5.5))
print(playdistance)