输入不断收到'str'

时间:2017-02-25 22:40:23

标签: python-3.x

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))

许多这些名称的背景和想法可能对你们很多人没有意义,但我的问题是当我完成输入时,它给了我错误“magicResist =(1 - (input1)*(input2 ) * (空值)) TypeError:不能将序列乘以'str'类型的非int“我需要帮助,我想知道为什么它认为它们是字符串,即使所有输入都追溯到带有定义变量的浮点数

2 个答案:

答案 0 :(得分:0)

输入从python2开始返回字符串。在此raw_input返回字符串之前,输入返回了评估值。 如果您希望评估输入,可以将输入与eval结合起来。

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

但是你应该设置传递给eval的locals和globals,以允许特定值或作为安全原因。

答案 1 :(得分:0)

输入返回一个字符串。如果您希望用户输入一个字符串(在您的示例中将是英雄或项目的名称)并想要使用之前计算的适当值,您可以使用字典:

# [...]
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

对项目执行相同操作,您可以使用变量来计算结果:

magicResist = (1 - hero * item * null)