打印语句不会打印我的变量的值

时间:2019-05-25 15:12:50

标签: python

所以我有这段代码来计算变量机会的值并打印出来,没有错误代码,但是当它到达打印语句时,无论我使用哪种编译器进行编译,输出中都不会出现

没有错误,所以我没什么可做的

import math

luck = 54
justice = 0
compassion = 1
bravery = 1
truth = 2
coreType = "Legendary"
baseChance = 0.01
element = "Light"

import math
valid = True
if coreType == "Common":
    coreRarity = 1
elif coreType == "Rare":
    coreRarity = 1.5
elif coreType == "Legendary":
    coreRarity = 3
else:
    print("Invalid core type")
    valid = False

if justice > compassion and justice > truth and justice > bravery:
    idea = "justice"
elif compassion > justice and compassion > truth and compassion > bravery:
    idea = "compassion"
elif truth > compassion and truth > justice and truth > bravery:
    idea = truth
elif bravery > compassion and bravery > truth and bravery > justice:
    idea = "bravery"
else:
    idea = "all"

if idea == "justice" and (element == "Light" or element == "Shadow"):
    boost = justice
elif idea == "truth" and (element == "Ice" or element == "Wind"):
    boost = truth
elif idea == "compassion" and (element == "Earth" or element == "Lightning"):
    boost = compassion
elif idea == "bravery" and (element == "Fire" or element == "Water"):
    boost = bravery
elif idea == "all":
    boost = bravery #basicly the above determines what the highest idea is and sets it but if the highest is all it means they are all the same so It doesn't matter one one the boost's value is cause its all the same

if valid == True:
    chance = math.max(math.sqrt(luck*0.01*1.3)+0.95,1)*0.01*(0.01*(100+5*idea)*coreRarity*baseChance)
    if coreType == "common":
        chance =* 0.25
    print(chance)

应该打印机会的价值,但是什么也不打印

2 个答案:

答案 0 :(得分:1)

您的代码有两个问题:

  • chance =* 0.25应该为chance *= 0.25here是所有Assignmet运算符的列表)

  • 模块math没有max属性,所以我建议您尝试使用max()而不是math.max()

那蜜蜂说,尝试用下面的语句替换当前的if语句(末尾的语句):

if valid == True:
    chance = max(math.sqrt(luck*0.01*1.3)+0.95,1)*0.01*(0.01*(100+5*idea)*coreRarity*baseChance)
    if coreType == "common":
        chance *= 0.25
    print(chance)

希望这会有所帮助

答案 1 :(得分:0)

可以通过多种方法来改进此代码:

设置coreRarity - 下面的代码:

valid = True
if coreType == "Common":
    coreRarity = 1
elif coreType == "Rare":
    coreRarity = 1.5
elif coreType == "Legendary":
    coreRarity = 3
else:
    print("Invalid core type")
    valid = False

可以做成字典查找

coreRarityDict = {
    "Common": 1,
    "Rare": 1.5,
    "Legendary": 3
}

要设置validcoreRarity,请使用可读的字典方法

valid = coreType in coreRarityDict
if valid:
    coreRarity = coreRarityDict[coreType] # dictionary lookup
else:
    print("Invalid core type")

设置思路-下一部分:

if justice > compassion and justice > truth and justice > bravery:
    idea = "justice"
elif compassion > justice and compassion > truth and compassion > bravery:
    idea = "compassion"
elif truth > compassion and truth > justice and truth > bravery:
    idea = truth
elif bravery > compassion and bravery > truth and bravery > justice:
    idea = "bravery"
else:
    idea = "all"

这里的逻辑基本上可以归结为justice, compassion, truth, bravery的(严格)最大值。如果存在最大的联系,请将主意设置为“全部”。 注意idea = truth看起来应该是idea = "truth"

再次使用字典

ideasDict = {
    "justice": justice,
    "compassion": compassion,
    "truth": truth,
    "bravery": bravery, 
}

要设定想法,我们希望键具有严格的最大值 请参阅-Getting key with maximum value in dictionary?

idea = max(ideasDict, key=lambda x: ideasDict[x])

要检查联系,我们可以计算该想法值在字典中出现的次数-请参阅Python 2.7 Counting number of dictionary items with given value

sum(1 for x in ideasDict.values() if x == idea)

结合起来,

idea = max(ideasDict, key=lambda x: ideasDict[x])
idea_freq = sum(1 for x in ideasDict.values() if x == idea)
if idea_freq > 1:
    idea = "all"

设置增强-此代码

if idea == "justice" and (element == "Light" or element == "Shadow"):
    boost = justice
elif idea == "truth" and (element == "Ice" or element == "Wind"):
    boost = truth
elif idea == "compassion" and (element == "Earth" or element == "Lightning"):
    boost = compassion
elif idea == "bravery" and (element == "Fire" or element == "Water"):
    boost = bravery
elif idea == "all":
    boost = bravery #basically the above determines what the highest idea is and sets it but if the highest is all it means they are all the same so It doesn't matter one one the boost's value is cause its all the same

请注意,您正在重复if idea == "xx": boost = xx 重用字典的代码看起来像

if idea == "justice" and (element == "Light" or element == "Shadow"):
    boost = ideasDict[idea]
elif idea == "truth" and (element == "Ice" or element == "Wind"):
    boost = ideasDict[idea]
elif idea == "compassion" and (element == "Earth" or element == "Lightning"):
    boost = ideasDict[idea]
elif idea == "bravery" and (element == "Fire" or element == "Water"):
    boost = ideasDict[idea]
elif idea == "all":
    boost = bravery #basically the above determines what the highest idea is and sets it but if the highest is all it means they are all the same so It doesn't matter one one the boost's value is cause its all the same

重复很多boost = ideasDict[idea]!让我们进一步简化!请注意,elif可以被设置为if,因为您的条件不重叠:idea不能同时为== "justice"== "truth"。让我们再次使用字典

elementsBoostDict = {
    "justice": ["Light", "Shadow"],
    "compassion": ["Ice", "Wind"],
    "truth": ["Earth", "Lightning"],
    "bravery": ["Fire", "Water"], 
}

if idea == "all":
    boost = bravery
elif element in elementsBoostDict[idea]:
    boost = ideasDict[idea]

可能未设置通知boost,例如idea == "justice"element == "ice",而boost不在代码中的其他地方使用吗?这是故意的吗?

计算机会-这是您的代码

if valid == True:
    chance = math.max(math.sqrt(luck*0.01*1.3)+0.95,1)*0.01*(0.01*(100+5*idea)*coreRarity*baseChance)
    if coreType == "common":
        chance =* 0.25
    print(chance)

注意idea是一个字符串(先前更正为TypeError: unsupported operand type(s) for +: 'int' and 'str'后出现错误idea = "truth"),应该用它的相对值代替。

if valid == True:行可以简化为if valid: 您的机会计算包括许多硬编码数字,可以简化0.01*0.01 = 0.0001。让我们将其移动到具有某些默认值的函数中。同样,由于luck是一个百分比,我们将使用luck=0.54并乘以0.01来去除乘法。

def chance_calc(coreType, coreRarity, idea, luck=0.54, baseChance=0.01):
    temp  = math.max(math.sqrt(luck * 1.3) + 0.95, 1)
    chance = temp * 0.0001 * (100 + 5 * ideasDict[idea]) * coreRarity * baseChance
    if coreType == "common":
        chance *= 0.25
    return chance

最终代码

import math

justice = 0
compassion = 1
bravery = 1
truth = 2
coreType = "Legendary"
element = "Light"

coreRarityDict = {
    "Common": 1,
    "Rare": 1.5,
    "Legendary": 3
}

ideasDict= {
    "justice": justice,
    "compassion": compassion,
    "truth": truth,
    "bravery": bravery, 
}

elementsBoost = {
    "justice": ["Light", "Shadow"],
    "compassion": ["Ice", "Wind"],
    "truth": ["Earth", "Lightning"],
    "bravery": ["Fire", "Water"], 
}

# set coreRarity
valid = coreType in coreRarityDict
if valid:
    coreRarity = coreRarityDict[coreType]
else:
    print("Invalid core type")

# set idea
idea = max(ideasDict, key=lambda x: ideasDict[x])
idea_freq = sum(1 for x in ideasDict.values() if x == idea)
if idea_freq > 1:
    idea = "all"

# set boost
if idea == "all":
    boost = bravery
elif element in elementsBoost[idea]:
    boost = ideasDict[idea]

def chance_calc(coreType, coreRarity, idea, luck=0.54, baseChance=0.01):
    temp  = math.max(math.sqrt(luck * 1.3) + 0.95, 1)
    chance = temp * 0.0001 * (100 + 5 * ideasDict[idea]) * coreRarity * baseChance
    if coreType == "common":
        chance *= 0.25
    return chance

if valid:
    chance = chance_calc(coreType, coreRarity, idea)
    print(chance)

# result 0.0005899919528666251

最终想法和验尸

我很有可能改变了您的意图或犯了一些错误。

使用像字典一样的结构化数据类型可以简化代码并提供我们可以使用的方法。另一种选择是使用可以组合ideasDictelementsBoost的类。

如果想法是“全部”,那么ideasDict[idea]将返回None,而chance_calc将返回错误。

boost未使用

如果ideasDict[idea]足够大,则机会可能大于1。

机会计算似乎可以改进。一个好主意可能是写下一堆justice, compassion, bravery, truth, coreType, element的组合以及您希望每个组合有什么机会,然后尝试编写一个与此相近的函数。