Python Forum
Calling a list troubles
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a list troubles
#5
The code feeds back into a discord command. I know this works, as my previous version of the bot works fine, but the json file was a mess and needed to be completely rewritten.

async def calc(ctx, quantity: float, itemA: str, itemB: str):
    itemAPrice = get_value(itemA)
    itemBPrice = get_value(itemB)
    if itemAPrice and itemBPrice:
        itemQuotient = itemAPrice/itemBPrice
        itemBEquivalent = round(quantity * itemQuotient, 2)
        quantity = quantity
        itemA = itemA
        itemBEquivalent = itemBEquivalent
        itemB = itemB
        author = ctx.message.author.name
        embed=discord.Embed(title="\N{CONVENIENCE STORE} Recommended Exchange Rate \N{CONVENIENCE STORE}", description=f'{quantity} {itemA} is equal to {itemBEquivalent} {itemB}', color=0xDA70D6)
        embed.set_footer(text=f"Requested by {author}")
        await ctx.send(embed=embed)
        await ctx.message.delete()
    elif not itemAPrice:
        await ctx.send('No match found for ' + itemA)
    elif not itemBPrice:
        await ctx.send('No match found for ' + itemB)
Previous version of it looked like this.

CONFIG = 'config.json'
with open(CONFIG) as json_data_file:
    config = json.load(json_data_file)

TOKEN = config['token']
items = config['items']
BOT_PREFIX = config['prefix']
aliases = config['aliases']

def get_value(item: str):
    if item:
        item = item.replace(" ", "").lower()
        try:
            return items[aliases[item]]
        except KeyError:
            return
    else:
        return None
with the json looking like this

https://pastebin.com/eNztKujJ
Reply


Messages In This Thread
Calling a list troubles - by giveen - Jan-11-2019, 07:09 PM
RE: Calling a list troubles - by micseydel - Jan-11-2019, 07:21 PM
RE: Calling a list troubles - by giveen - Jan-11-2019, 07:26 PM
RE: Calling a list troubles - by nilamo - Jan-11-2019, 07:37 PM
RE: Calling a list troubles - by giveen - Jan-11-2019, 07:44 PM
RE: Calling a list troubles - by nilamo - Jan-11-2019, 07:50 PM
RE: Calling a list troubles - by nilamo - Jan-11-2019, 07:52 PM
RE: Calling a list troubles - by giveen - Jan-11-2019, 08:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Troubles with program Harambe 37 9,257 May-28-2020, 05:12 PM
Last Post: GOTO10
  Troubles on how to use Open CV knowledge1st 1 2,504 May-23-2018, 05:57 PM
Last Post: Larz60+
  Calling a function to return a list of percentages Liquid_Ocelot 7 6,320 Mar-25-2017, 01:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020