Python - 获取具有最高整数的变量名称

时间:2017-06-04 17:31:12

标签: python

在为数字添加数字之后,如何获得具有最高整数的变量?

g_dirt4 = 0
g_destiny2 = 0
g_southpark = 0
g_codww2 = 0
g_bfront2 = 0
g_reddead2 = 0
g_fifa18 = 0
g_motogp17 = 0
g_elderscrolls = 0
g_crashbandicoot = 0

#Right here are a lot of if statements which add += 1 to the specific variables

if request.method == "POST":
    #I would like it to print like this:
    print(Highest variable name, corresponding number)

我该怎么做?

编辑:

这是我的整个剧本

from flask import Flask, render_template, request, redirect

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])



def main():

    g_dirt4 = 0
    g_destiny2 = 0
    g_southpark = 0
    g_codww2 = 0
    g_bfront2 = 0
    g_reddead2 = 0
    g_fifa18 = 0
    g_motogp17 = 0
    g_elderscrolls = 0
    g_crashbandicoot = 0

    d = {'g_dirt4': g_dirt4, 'g_destiny2': g_destiny2, 'g_southpark': g_southpark, 'g_codww2': g_codww2, 'g_bfront2': g_bfront2, 'g_reddead2': g_reddead2, 'g_fifa18': g_fifa18, 'g_motogp17': g_motogp17, 'g_elderscrolls': g_elderscrolls, 'g_crashbandicoot': g_crashbandicoot}

    if request.method == "GET":
        return render_template("main_page.html")

    if (request.form["console"] == "PC"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["console"] == "PS4"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["console"] == "Xbox One"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["console"] == "Maakt niet uit"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 3 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 7 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 12 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 16 jaar"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 18 jaar"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["genre"] == "Shooter"):
        #g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Sports"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Role-playing"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Platform"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["genre"] == "Simulation"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["multiplayer"] == "Ja"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["multiplayer"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["splitscreen"] == "Ja"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["splitscreen"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["geweld"] == "Ja"):
        #g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["geweld"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["interesse"] == "balsport"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "motorsport"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "geschiedenis/oorlog"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "films/series"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "fantasie/fictie"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["interesse"] == "avonturen"):
        #g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["competitief"] == "Ja"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["competitief"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "single player storyline hebben"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "single player open-world hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "multiplayer storyline hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "multiplayer open-world hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Juni 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Juli 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Augustus 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "September 2017"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Oktober 2017"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "November 2017 of later"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1



    if request.method == "POST":
        print(max(d, key=d.get))
        print("g_dirt4", g_dirt4, "g_destiny2", g_destiny2, "g_southpark", g_southpark, "g_codww2", g_codww2, "g_bfront2", g_bfront2, "g_reddead2", g_reddead2, "g_fifa18", g_fifa18, "g_motogp17", g_motogp17, "g_elderscrolls", g_elderscrolls, "g_crashbandicoot", g_crashbandicoot)
        return redirect("https://i.vimeocdn.com/portrait/8487168_300x300")

我不知道是否有更简单的方法来解决这个问题,但这对我来说也很有用。它花了一些时间。

1 个答案:

答案 0 :(得分:2)

使用字典:

g_dirt4 = 0
g_destiny2 = 0
g_southpark = 0
...

d = {'g_dirt4': g_dirt4, 'g_destiny2': g_destiny2, 'g_southpark': g_southpark}

print(max(d.items(), key=lambda x: x[1]))