In python when I try this:
admin = "Vaibhav"
def print_admin(default):
# global admin
if default == "default":
print(admin)
else:
admin = "other"
print(admin)
print_admin("")
print_admin("default")
It gives me an error:
error:UnboundLocalError: local variable 'admin' referenced before assignment
If i use the global keyword then it only uses global in both cases. I want to be able to use local("other") if "default" is not given in the parameters and global("Vaibhav") if it is.
This works perfectly in javascript but not in python.
Javascript code for doing the same:
let admin_name = "Vaibhav";
function printAdminName(admin_name_default) {
if (admin_name_default != "default") {
let admin_name = "Other";
console.log(admin_name)
}
else {
console.log(admin_name);
}
}
printAdminName("default");
printAdminName();
output: Vaibhav other
** I also wanted to know why this works in javascript and not python, since both are very similar languages. I wanted to know what concept makes this difference. As i spent a lot of time to logically figure it out.