Code Club – Python functions 2

So to follow the previous post, we are going to look at passing 2 arguments to a function, adding them together, then displaying the result.

Firstly open a new Python Trinket

Firstly we are going to tell the interpreter to use python3.

#!/usr/bin/env python3 #use python 3

Now we create a function to take two arguments and display the sub of both

def add_numbers(x,z):
  #print(x)
  #print(z)
  add = (int(x) + int(z))
  print ('Total = ')
  print(add)

Get user input

x = input("First Number") # ask for first number
z = input("Second Number") # ask for second number

Convert to integers

int(x)
int(z)

Call function and pass values to function

add_numbers(x,z)

Tags

#CodeClub,#Python.#Programming,#Functions,#Arguments


MastodonPeertubeJoin Mastodon

AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay