+ 1
How to create multiple methods in python?
i've recently learned classes and functions, and as a little practice task I've tried to create a class which asks the user for their name, and would display their balance. but I'm a little confused when it comes to the parameters, as well as calling the function. could someone help? https://code.sololearn.com/cMrXcECagtL8/?ref=app
1 Respuesta
+ 1
class bank:
def __init__(self,name,balance):
self.name = name
self.balance = balance
c1 = bank("Naair", "50")
def login(x):
if x==c1.name:
print("{}".format(c1.balance))
log = input("enter your name: ")
login(log)
You can have the input inside your function if you want, but if you are going to call the value as an argument when you call the function, better to out it outside. Here I get the user name as a variable called log.. then I call the function, passing in log as an argument. I changed the argument of the function itself to x just to show that is doesn't matter what you name that variable. I can pass log when I call the function and it becomes x. then the function uses it as x.
Alternatively you can have the input inside the function and skip the argument all together.
class bank:
def __init__(self,name,balance):
self.name = name
self.balance = balance
c1 = bank("Naair", "50")
def login():
log = input("enter your name: ")
if x==c1.name:
print("{}".format(c1.balance))
login()