Jump to content

Add an if statment to everything at once?

Need_Not
Go to solution Solved by Sauron,
27 minutes ago, Need_Not said:

So i was wonding if it's possible to put a 0 on all stats that dont exist with like 2 lines of code?

No.

 

By the way, your code is completely unreadable - the variable names are meaningless and you didn't really need intermediary variable names for most of them to begin with.

 

To elaborate on what @minibois suggested, think of something like this:

def getInfo(json, param):
  try:
    return int(json['stats']['Bedwars'][param])
  except:
    return 0

    
...

print("Kills: %d" % (getInfo(Player.JSON, "eight_one_kills_bedwars")))
print("K/D: %.2f" % (getInfo(Player.JSON, "eight_one_kills_bedwars")/getInfo(Player.JSON, "eight_one_deaths_bedwars")))

 

Ok I am making this python program that lists stats of anyone who has ever join the minecraft server hypixel (yeah ik but i'm board) using their api

So I coded the stats for the first game like this 

import hypixel
import tkinter as tk
from tkinter import simpledialog

ROOT = tk.Tk()

ROOT.withdraw()
# the input dialog
USER_INP = simpledialog.askstring(title="Test",
                                  prompt="Enter a Player name or UUID:")




API_KEYS = ['no key 4 u']
hypixel.setKeys(API_KEYS)

Player = hypixel.Player(USER_INP)

Guild = hypixel.Guild('Bad Wifi')

PlayerName = Player.getName() 
print("Player name: ", end='')
print(PlayerName)

PlayerRank = Player.getRank()
print("rank: ", end='')
print(PlayerRank['rank'])

PlayerLevel = Player.getLevel()
print("level: ", end='')
print(PlayerLevel) 

playerInfo = Player.getPlayerInfo()
print('UUID' + ": ", end='')
print(playerInfo['uuid'])


GuildID = Player.getGuildID()
print('Guild: ', end='')
print(GuildID)

print("")
print("Bedwars")
print("")

playerJSON = Player.JSON
playerJSON['stats']['Bedwars']

playerJSON = Player.JSON
playerJSON['achievements']
print('Bedwars Level: ', end='')
bwlevel = (playerJSON['achievements']['bedwars_level'])
cbwlevel = '{:,}'.format(bwlevel)
print(cbwlevel)

print('Coins: ', end='')
bwcoins = (playerJSON['stats']['Bedwars']['coins'])
cbwcoins = '{:,}'.format(bwcoins)
print(cbwcoins)

print('Winstreak: ', end='')
ws = (playerJSON['stats']['Bedwars']['winstreak'])
cws = '{:,}'.format(ws)
print(cws)

print('')

print('Iron Collected: ', end='')
ironss = (playerJSON['stats']['Bedwars']['iron_resources_collected_bedwars'])
ciron = '{:,}'.format(ironss)
print(ciron)

print('Gold Collected: ', end='')
goldd = (playerJSON['stats']['Bedwars']['gold_resources_collected_bedwars'])
cgold = '{:,}'.format(goldd)
print(cgold)

print('Diamonds Collected: ', end='')
diamondss = (playerJSON['stats']['Bedwars']['diamond_resources_collected_bedwars'])
cdiamond = '{:,}'.format(diamondss)
print(cdiamond)

print('Emeralds Collected: ', end='')
ems = (playerJSON['stats']['Bedwars']['emerald_resources_collected_bedwars'])
cems = '{:,}'.format(ems)
print(cems)



print('')

print("SOLO:")


playerJSON = Player.JSON
playerJSON['stats']['Bedwars']
print('Kills: ', end='')
bw = (playerJSON['stats']['Bedwars']['eight_one_kills_bedwars'])
cbw = '{:,}'.format(bw)
print(cbw)

print('Deaths: ', end='')
bw1 = (playerJSON['stats']['Bedwars']['eight_one_deaths_bedwars'])
cbw1 = '{:,}'.format(bw1)
print(cbw1)

e = int(playerJSON['stats']['Bedwars']['eight_one_deaths_bedwars'])
f = int(playerJSON['stats']['Bedwars']['eight_one_kills_bedwars'])
g =  f / e

print('K/D Ratio: ', end='')
cg = '{:,.2f}'.format(g)
print(cg)

print("")

print('Final Kills: ', end='')
bw2 = (playerJSON['stats']['Bedwars']['eight_one_final_kills_bedwars'])
cbw2 = '{:,}'.format(bw2)
print(cbw2)


print('Final Deaths: ', end='')
bw3 = (playerJSON['stats']['Bedwars']['eight_one_final_deaths_bedwars'])
cbw3 = '{:,}'.format(bw3)
print(cbw3)

a = int(playerJSON['stats']['Bedwars']['eight_one_final_deaths_bedwars'])
b = int(playerJSON['stats']['Bedwars']['eight_one_final_kills_bedwars'])
c = b / a
print('Final K/D Ratio: ', end='')
cc = '{:,.2f}'.format(c)
print(cc)

print("")

print('Wins: ', end='')
bw4 = (playerJSON['stats']['Bedwars']['eight_one_wins_bedwars'])
cbw4 = '{:,}'.format(bw4)
print(cbw4)

print('Losses: ', end='')
bw5 = (playerJSON['stats']['Bedwars']['eight_one_losses_bedwars'])
cbw5 = '{:,}'.format(bw5)
print(cbw5)

h = int(playerJSON['stats']['Bedwars']['eight_one_wins_bedwars'])
i = int(playerJSON['stats']['Bedwars']['eight_one_losses_bedwars'])
j = h / i

print('W/L ratio: ', end='')
cj = '{:,.2f}'.format(j)
print(cj)

print("")
print("DOUBLES:")

playerJSON = Player.JSON
playerJSON['stats']['Bedwars']
print('Kills: ', end='')
bw6 = (playerJSON['stats']['Bedwars']['eight_two_kills_bedwars'])
cbw6 = '{:,}'.format(bw6)
print(cbw6)

print('Deaths: ', end='')
bw7 = (playerJSON['stats']['Bedwars']['eight_two_deaths_bedwars'])
cbw7 = '{:,}'.format(bw7)
print(cbw7)

e1 = int(playerJSON['stats']['Bedwars']['eight_two_deaths_bedwars'])
f1 = int(playerJSON['stats']['Bedwars']['eight_two_kills_bedwars'])
g1 =  f1 / e1

print('K/D Ratio: ', end='')
cg1 = '{:,.2f}'.format(g1)
print(cg1)


print("")

print('Final Kills: ', end='')
bw8 = (playerJSON['stats']['Bedwars']['eight_two_final_kills_bedwars'])
cbw8 = '{:,}'.format(bw8)
print(cbw8)


print('Final Deaths: ', end='')
bw9 = (playerJSON['stats']['Bedwars']['eight_two_final_deaths_bedwars'])
cbw9 = '{:,}'.format(bw9)
print(cbw9)


a1 = int(playerJSON['stats']['Bedwars']['eight_two_final_deaths_bedwars'])
b1 = int(playerJSON['stats']['Bedwars']['eight_two_final_kills_bedwars'])
c1 = b1 / a1
print('Final K/D Ratio: ', end='')
cc1 = '{:,.2f}'.format(c1)
print(cc1)


print("")

print('Wins: ', end='')
bw10 = (playerJSON['stats']['Bedwars']['eight_two_wins_bedwars'])
cbw10 = '{:,}'.format(bw10)
print(cbw10)


print('Losses: ', end='')
bw11 = (playerJSON['stats']['Bedwars']['eight_two_losses_bedwars'])
cbw11 = '{:,}'.format(bw11)
print(cbw11)

h1 = int(playerJSON['stats']['Bedwars']['eight_two_wins_bedwars'])
i1 = int(playerJSON['stats']['Bedwars']['eight_two_losses_bedwars'])
j1 = h1 / i1

print('W/L ratio: ', end='')
cj1 = '{:,.2f}'.format(j1)
print(cj1)

print("")
print("3v3v3v3:")

playerJSON = Player.JSON
playerJSON['stats']['Bedwars']
print('Kills: ', end='')
bw12 = (playerJSON['stats']['Bedwars']['four_three_kills_bedwars'])
cbw12 = '{:,}'.format(bw12)
print(cbw12)

print('Deaths: ', end='')
bw13 = (playerJSON['stats']['Bedwars']['four_three_deaths_bedwars'])
cbw13 = '{:,}'.format(bw13)
print(cbw13)

e2 = int(playerJSON['stats']['Bedwars']['four_three_deaths_bedwars'])
f2 = int(playerJSON['stats']['Bedwars']['four_three_kills_bedwars'])
g2 =  f2 / e2

print('K/D Ratio: ', end='')
cg2 = '{:,.2f}'.format(g2)
print(cg2)

print("")

print('Final Kills: ', end='')
bw13 = (playerJSON['stats']['Bedwars']['four_three_final_kills_bedwars'])
cbw13 = '{:,}'.format(bw13)
print(cbw13)

print('Final Deaths: ', end='')
bw14 = (playerJSON['stats']['Bedwars']['four_three_final_deaths_bedwars'])
cbw14 = '{:,}'.format(bw14)
print(cbw14)

a2 = int(playerJSON['stats']['Bedwars']['four_three_final_deaths_bedwars'])
b2 = int(playerJSON['stats']['Bedwars']['four_three_final_kills_bedwars'])
c2 = b2 / a2
print('Final K/D Ratio: ', end='')
cc2 = '{:,.2f}'.format(c2)
print(cc2)

print("")

print('Wins: ', end='')
bw15 = (playerJSON['stats']['Bedwars']['four_three_wins_bedwars'])
cbw15 = '{:,}'.format(bw15)
print(cbw15)

print('Losses: ', end='')
bw16 = (playerJSON['stats']['Bedwars']['four_three_losses_bedwars'])
cbw16 = '{:,}'.format(bw16)
print(cbw16)


h2 = int(playerJSON['stats']['Bedwars']['four_three_wins_bedwars'])
i2 = int(playerJSON['stats']['Bedwars']['four_three_losses_bedwars'])
j2 = h2 / i2

print('W/L ratio: ', end='')
cj2 = '{:,.2f}'.format(j2)
print(cj2)

print("")
print("4v4v4v4:")

playerJSON = Player.JSON
playerJSON['stats']['Bedwars']
print('Kills: ', end='')
bw17 = (playerJSON['stats']['Bedwars']['four_four_kills_bedwars'])
cbw17 = '{:,}'.format(bw17)
print(cbw17)


print('Deaths: ', end='')
bw18 = (playerJSON['stats']['Bedwars']['four_four_deaths_bedwars'])
cbw18 = '{:,}'.format(bw18)
print(cbw18)

e3 = int(playerJSON['stats']['Bedwars']['four_four_deaths_bedwars'])
f3 = int(playerJSON['stats']['Bedwars']['four_four_kills_bedwars'])
g3 =  f3 / e3

print('K/D Ratio: ', end='')
cg3 = '{:,.2f}'.format(g3)
print(cg3)

print("")

print('Final Kills: ', end='')
bw19 = (playerJSON['stats']['Bedwars']['four_four_final_kills_bedwars'])
cbw19 = '{:,}'.format(bw19)
print(cbw19)

print('Final Deaths: ', end='')
bw20 = (playerJSON['stats']['Bedwars']['four_four_final_deaths_bedwars'])
cbw20 = '{:,}'.format(bw20)
print(cbw20)

a3 = int(playerJSON['stats']['Bedwars']['four_four_final_deaths_bedwars'])
b3 = int(playerJSON['stats']['Bedwars']['four_four_final_kills_bedwars'])
c3 = b3 / a3
print('Final K/D Ratio: ', end='')
cc3 = '{:,.2f}'.format(c3)
print(cc3)

print("")

print('Wins: ', end='')
bw21 = (playerJSON['stats']['Bedwars']['four_four_wins_bedwars'])
cbw21 = '{:,}'.format(bw21)
print(cbw21)

print('Losses: ', end='')
bw22 = (playerJSON['stats']['Bedwars']['four_four_losses_bedwars'])
cbw22 = '{:,}'.format(bw22)
print(cbw22)

h3 = int(playerJSON['stats']['Bedwars']['four_four_wins_bedwars'])
i3 = int(playerJSON['stats']['Bedwars']['four_four_losses_bedwars'])
j3 = h3 / i3

print('W/L ratio: ', end='')
cj3 = '{:,.2f}'.format(j3)
print(cj3)

print("")

print("Overall:")

ki55 = f + f1 + f2 + f3
de4 = e + e1 + e2 + e3
kd54 = ki55 / de4
fki55 = b + b1 + b2 + b3
fde4 = a + a1 + a2 + a3
fkd54 = fki55 / fde4
aw = h + h1 + h2 + h3
al = i + i1 + i2 + i3
wl = aw / al

print('Kills: ', end='')
cki55 = '{:,}'.format(ki55)
print(cki55)

print('Deaths: ', end='')
cde4 = '{:,}'.format(de4)
print(cde4)

print('K/D Ratio: ', end='')
ckd54 = '{:,.2f}'.format(kd54)
print(ckd54)

print("")

print('Final kills: ', end='')
cfki55 = '{:,}'.format(fki55)
print(cfki55)

print('Final Deaths: ', end='')
cfde4 = '{:,}'.format(fde4)
print(cfde4)

print('Final K/D Ratio: ', end='')
cfkd54 = '{:,.2f}'.format(fkd54)
print(cfkd54)


print("")

print('Wins: ', end='')
caw = '{:,}'.format(aw)
print(caw)

print('Losses: ', end='')
cal = '{:,}'.format(al)
print(cal)

print('W/L Ratio: ', end='')
cwl = '{:,.2f}'.format(wl)
print(cwl)

from tkinter import *

master = Tk()

w = Label(master, text=cws)
w.pack()

mainloop()

and it out puts stats as it should but the the only issue is... if a player has never gotten a kill or even never played that game the stat that this api wrapper is requesting something that isn't in the api meaning it just spits out errors instead of a 0

so that's where I need your help! I was told that I need to add if and or else statments so like if it doesn't get a responce from that stat it will display 0 instead of an error but here is the thing! as you can see I have already done a ton and going back and redoing each print would take forever nor do I even know how!

 

So i was wonding if it's possible to put a 0 on all stats that dont exist with like 2 lines of code?

Just some random guy who has a gaming computer to play on his Minecraft server

Link to comment
Share on other sites

Link to post
Share on other sites

This is why you first structure code and make it so your code complies to the single responsibility pattern, so you can easily change pieces of code later

 

You could do a quick and really dirty fix of just placing a try catch around your code, but I would very much advice against this, as it would just create awful code.

A more pretty way of going about this is making a method called "retrieveInfo", that returns data (int probably?) and receives a request as a parameter. That way you only have 1 method that retrieves data, so only 1 place to create restrictions on the sort of data you would want to receive.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, Need_Not said:

So i was wonding if it's possible to put a 0 on all stats that dont exist with like 2 lines of code?

No.

 

By the way, your code is completely unreadable - the variable names are meaningless and you didn't really need intermediary variable names for most of them to begin with.

 

To elaborate on what @minibois suggested, think of something like this:

def getInfo(json, param):
  try:
    return int(json['stats']['Bedwars'][param])
  except:
    return 0

    
...

print("Kills: %d" % (getInfo(Player.JSON, "eight_one_kills_bedwars")))
print("K/D: %.2f" % (getInfo(Player.JSON, "eight_one_kills_bedwars")/getInfo(Player.JSON, "eight_one_deaths_bedwars")))

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Sauron said:

No.

 

By the way, your code is completely unreadable - the variable names are meaningless and you didn't really need intermediary variable names for most of them to begin with.

 

To elaborate on what @minibois suggested, think of something like this:


def getInfo(json, param):
  try:
    return int(json['stats']['Bedwars'][param])
  except:
    return 0

    
...

print("Kills: %d" % (getInfo(Player.JSON, "eight_one_kills_bedwars")))
print("K/D: %.2f" % (getInfo(Player.JSON, "eight_one_kills_bedwars")/getInfo(Player.JSON, "eight_one_deaths_bedwars")))

 

TY so much it worked and... you showed me how to clean up my code

Just some random guy who has a gaming computer to play on his Minecraft server

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Sauron said:

No.

 

By the way, your code is completely unreadable - the variable names are meaningless and you didn't really need intermediary variable names for most of them to begin with.

 

To elaborate on what @minibois suggested, think of something like this:


def getInfo(json, param):
  try:
    return int(json['stats']['Bedwars'][param])
  except:
    return 0

    
...

print("Kills: %d" % (getInfo(Player.JSON, "eight_one_kills_bedwars")))
print("K/D: %.2f" % (getInfo(Player.JSON, "eight_one_kills_bedwars")/getInfo(Player.JSON, "eight_one_deaths_bedwars")))

 

here is the thing... it divides by 0

Just some random guy who has a gaming computer to play on his Minecraft server

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Need_Not said:

here is the thing... it divides by 0

Then you should check for that situation... depending on how you want to count K/D. If you just want it to be the number of kills when the player has 0 deaths then you can check for 0 with max like so

print("K/D: %.2f" % (getInfo(Player.JSON, "eight_one_kills_bedwars")/max(getInfo(Player.JSON, "eight_one_deaths_bedwars"), 1)))

this way it will use the highest number between the number of deaths and 1.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×