Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do the variables work?
#1
Here's the snippet;
oneday = [20.00, 12.00, 16.00, 60.00, 15.00]
twoday = [30.00, 18.00, 24.00, 90.00, 22.50]

vdays = 1
ttype = 0

global ticket_cost
ticket_cost = 0

def func_adult_tic():
    adult = int(input('Enter No. of adult tickets needed    : '))
    
    if vdays == 1:
        ticket_cost += (adult * float(oneday[ttype]))
    else:
        ticket_cost += (adult * float(twoday[ttype]))


func_adult_tic()
print(ticket_cost)
Why doesnt the value of ticket_cost change?
I tried many ways, but to no avail.

The main goal:
Get value of adults,
Multiply it with the price in the list,
Output ticket cost.

Thanks for helping, Kind soul.
Reply
#2
Hi, its me again,
I found out the error.
You return the value from the function, and assign it again out of the function.

Thanks...
Reply
#3
(Apr-08-2022, 05:57 AM)Oshadha Wrote: You return the value from the function, and assign it again out of the function.
that is the better approach, pass arguments and return values.
Just FYI, what is the problem with your code using global is that you have to declare the variable global inside the function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Apr-08-2022, 06:40 AM)buran Wrote:
(Apr-08-2022, 05:57 AM)Oshadha Wrote: You return the value from the function, and assign it again out of the function.
that is the better approach, pass arguments and return values.
Just FYI, what is the problem with your code using global is that you have to declare the variable global inside the function

Exactly, I thought if I declare the variable with global, it would work inside the function...
Reply
#5
global variable tells Python to use "variable" in the global scope. When you did this:
global ticket_cost
This code is already in the global (module) scope, so it has no effect at all. You need to use the global keyword inside the function.
def func_adult_tic():
    global ticket_cost
    adult = int(input('Enter No. of adult tickets needed    : '))
     
    if vdays == 1:
        ticket_cost += (adult * float(oneday[ttype]))  # No need for float()
    else:
        ticket_cost += (adult * float(twoday[ttype]))
Global variables are bad. Sometimes they are needed, but avoid using global variables whenever possible. Programs with many global variables become difficult to maintain. Even minor changes become difficult because you have to evaluate how changing a global variable inside a function might affect other code that uses the same variable. Your new design with the return value is much better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to work with variables changed in definitions Prof_Jar_Jar 2 2,578 Dec-16-2018, 12:04 AM
Last Post: Prof_Jar_Jar

Forum Jump:

User Panel Messages

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