Home > Blockchain >  Replace input to variable in function python
Replace input to variable in function python

Time:05-05

I have a function

def test(z, x, c):
    a = int(input('First number:    '))
    b = int(input('Second number:    '))
    c = int(input('Third number:    '))
    if a > b and a > c:
        print('Max number:     ', a)
    elif b > c:
        print('Max number:     ', b)
    else:
        print('Max number:     ', c)

Next, I call the function with attributes

print('1 test: 1, 2, 3')
test(1, 2, 3)
print('2 test: 1, 3, 2')
test(1, 3, 2)

How to automatically replace variables (a, b, c) with passed parameters (z, x, c) inside a function so that the input is not called. May be use something decorator?

CodePudding user response:

def test(a, b, c):
    if a > b and a > c:
        print('Max number:     ', a)
    elif b > c:
        print('Max number:     ', b)
    else:
        print('Max number:     ', c)

you mean like so? You probably don't want to ask for input IN your function, you want to pass the values, right?

CodePudding user response:

try this. definitely faster, easier to remember and more often used in practice ;] '''

def check(your_list): maximum = max(your_list) print('The highest number you have given is: ', maximum) a = int(input('1st number: ')) b = int(input('2nd number: ')) c = int(input('3rd number: ')) check([a, b, c]) '''

CodePudding user response:

def test(x, y, z):
    if x > y and x > z:
        print('Max number:     ', x)
    elif y > z:
        print('Max number:     ', y)
    else:
        print('Max number:     ', z)

a = int(input('First number:    '))
b = int(input('Second number:    '))
c = int(input('Third number:    '))

test(a, b, c)

But I would instead use the inbuilt max() function so

def test(list):
    highest_number = max(list)
    print('Max number:     ', highest_number)

a = int(input('First number:    '))
b = int(input('Second number:    '))
c = int(input('Third number:    '))

test([a, b, c])
  • Related