Seba
Seba

Reputation: 11

How to print argument from created function?

I created a function returning the highest value by using max(). As arguments, I inserted a few operations eg. 2+2, 3+5, 5-1. The max() shows the highest value reflecting as a score. How to retrieve an operation and score?

def largest_num(*args):
    print(max(args))

largest_num(2+1,30-24,8*3)

I would like to retrieve a message: "max value is 8*3 = 24"

Upvotes: 0

Views: 69

Answers (1)

eumiro
eumiro

Reputation: 212835

You're not passing 8*3 to your method. You're passing 24. So if you want to have the original information, you can pass it as a string "8*3", ast.literal_eval it in the method to get 24 and display the original string.

import ast

def largest_num(*args):
    max_item = max(args, key=ast.literal_eval)
    print(f"max value is {max_item} = {ast.literal_eval(max_item)}")

largest_num("2+1", "30-24", "8*3")

prints

max value is 8*3 = 24

Upvotes: 2

Related Questions