Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code Questions.
#1
Hi everyone.
First and foremost I should say that I am a beginner and right now I am analyzing and trying to understand this code. As you can see is a kind of ATM. I see that is made of a bunch of functions to tackle all the logic. I have looked for information about some terms (return, True, False, not ok) and I have read and understood the
definition. But it´s kind of difficult to understand How it works in a code. I need to be clear with that to continue.

When I end a function with "return True" and/or "return False", what does these exactly mean? where those values store? (it supposed that "return" send a value but we need a variable to store it) That happen for example in
def userValidation(u,c); def withdraw(value); def deposit(value); def action(option)

In def deploy() I don´t understand what is the purpose of the line "ok, balanceInBd = action(option)".

Maybe for most of you my questions have obvious answers, but as I told you, I am a beginner and I am learning by reading and coding.

Thanks in advance.

PD. English in process, sorry for grammar mistakes.

def login():
	user=input("User: ")
	password=input("Password: ")
	return userValidation(user,password)

def userValidation(u,c):
    if u.lower()==userInBd and c==passwordInBd:
        return True
    return False 

def deploy():
    if not login():
        print("Invalid user or Password")
        return
    print("Choose a operation: ")
    option = int(input("1. Deposit or 2. Withdraw: "))
    ok, balanceInBd = action(option)
    if not ok:
        print("Not enough money. Balance: ", balanceInBd)
    else:
        print("Successful Process. New Balance: ", balanceInBd)

def withdraw(value):
    if value>balanceInBd:
        return False, balanceInBd
    return True, balanceInBd-value

def deposit(value):
    return True, balanceInBd+value

def action(option):
    if option ==1:
        value = int(input("Deposit amount: "))
        return deposit(value)
    if option==2:
        value = int(input("Withdraw amount: "))
        return withdraw(value)
    return False, balanceInBd

deploy()
Reply
#2
When a function returns a value, you can store that value in a variable or you can act on it directly. Looking at the start of the deploy() function, you see if not login():, which could informally be expressed as "if the value returned by login() is not True". This line calls the function login(), which in turn calls the userValidation() function and returns the value it gets back. So, userValidation() returns either True or False, and login() then returns that value to deploy().

The line ok, balanceInBd = action(option) is assigning two variables (note that the action() function returns two values). So it assigns the first value returned to the variable "ok", and the second value returned to "balanceInBd".

This ATM code might be a little difficult to follow for someone just starting out, and it can't run to completion as currently presented since userInBd, passwordInBd, and balanceInBd are not defined. Here is some simpler code to help illustrate how a return value can be used:
# define a function that returns the string 'Hello'
def returnHello():
    return 'Hello'

# call returnHello() and use the return value directly in a print statement
print(returnHello())

# call returnHello() and assign the return to a variable for later use
var = returnHello()
print(var)
Something worth noting is that ALL functions in Python return a value. If the function definition doesn't specify a return value, then the value returned is None.
Reply
#3
Dear Goto10, thanks for your answer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Questions re: my first python app (GUI, cross-platform, admin/root-level commands)? DonnyBahama 0 1,702 Feb-27-2020, 08:14 PM
Last Post: DonnyBahama
  3 easy questions for python programmers but 3 pain in the ass for me grjmmr 6 3,910 Jul-22-2018, 08:41 PM
Last Post: Larz60+
  VS Code questions Larz60+ 3 2,779 Jun-06-2018, 09:23 AM
Last Post: Larz60+
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,430 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020