0
base = (.75)
hood = (.70)
pipeAura = (.9)
cloak = (.85)
glimmer = (.85)
null = (.78)
heap = (.88)
corrosive = (.75)
spiritBear = (.67)
spellShield = (.5)
berserkersBlood = (.5)
pipe = hood * pipeAura 
antimage = spellShield * base
viper = corrosive * base
huksar = berserkersBlood * base
meepo = (.65) * base
veil = (1.25)
pudge = heap * base

input1 = input('What hero are you trying to kill?(antimage, viper, huskar, meepo, pudge)')
input2 = input('What item is the hero holding/using/affected by? (hood, pipeAura, cloak, glimmer, pipe, veil)') 
input3 = input('is the hero affected by null field? (yes/no)')
userHealth = input("what is the hero's current hp?")

if input3 == null:
    null = (.78)
else:
    null = 1

magicResist = (1 - (input1) * (input2) * (null))

The context of many of these names and the idea may not make sense to many of you, but my problem is when i finish giving the input, it gives me the error" magicResist = (1 - (input1) * (input2) * (null)) TypeError: can't multiply sequence by non-int of type 'str'" I need help with this, and i am wondering why it considers them strings, even though all of the inputs trace back to floats with the defined variables

zach l
  • 1
  • `null`? And `input` indeed always returns a `str`. You can use `float(..)` to convert it to a float. – Willem Van Onsem Feb 25 '17 at 22:42
  • I attempted to do this by changing the final line in the code, magicResist = (1 - float(input1) * float(input2) * (null)) and it said it couldnt convert to a float, even though it is a variable that represents a float. exact error: ValueError: could not convert string to float: 'antimage' – zach l Feb 25 '17 at 22:45
  • @ZachI: but you need to convert `input3` to float in the `if` statement: `if float(input3) == null:`... – Willem Van Onsem Feb 25 '17 at 22:49
  • that isnt the problem im having, that part is fine, its with the last line, even though i changed it to magicResist = (1 - float(input1) * float(input2) * (null)), it still says (for input1) "could not convert string to float: 'antimage'", even though i set it to a float and 'antimage' is assigned to a variable – zach l Feb 25 '17 at 22:57
  • @zachl: the conversion to float is tried on the string the user entered. See my answer if you want the user to enter e.g. the name of a hero and your code to use the appropriate value – tschale Feb 25 '17 at 23:27

2 Answers2

0

Input returns strings since python2. Before this raw_input returned a string and input returned the evaluated value. If you want your input be evaluated you could combine input with eval.

inputN = eval(input("message..."))

But you should set the locals and globals passed to eval, to allow specific values or as security-reasons.

cmdLP
  • 1,658
  • 9
  • 19
  • It is insecure unless you specify the globals and locals. Just use the dict of the variables above and pass it as globals. -- see `help(eval)` – cmdLP Feb 26 '17 at 11:53
  • no that is not the problem. The problem is that here, you give a hacker full access to your program because they can execute arbitrary code. What if the user mistypes for instance and says `list = 4`. From now on your program cannot call the `list(..)` constructor anymore. One should **never** use `eval(..)` unless there is no other choice (and you have looked for alternatives for some time). – Willem Van Onsem Feb 26 '17 at 12:17
  • When you set the globals, each of them would be copied by setting "__builtins__"-entry to an copy of the __builtins__-dict. I will see and test what you can do for secure with eval... – cmdLP Feb 26 '17 at 23:05
  • you [cannot secure `eval`](http://stackoverflow.com/questions/661084/security-of-pythons-eval-on-untrusted-strings). – Willem Van Onsem Feb 26 '17 at 23:39
0

Input returns a string. If you want the user to enter a string (in your example that would be the name of the hero or the item) and want to use the appropriate value you calculated before, you might use a dictionary:

# [...]
antimage = spellShield * base
viper = corrosive * base
# [...]
heroes = {
    'antimage': antimage,
    'viper': viper,
    # and so on
}
input1 = input('What hero are you trying to kill?(antimage, viper, huskar, meepo, pudge)')
hero = heroes.get(hero, 1)  # I specify 1 as the default if the key is not present in the dictionary, you might specify a more suitable value

Do the same for the items and you can use the variables to calculate the result:

magicResist = (1 - hero * item * null)
tschale
  • 975
  • 1
  • 18
  • 34