Python Forum
not able to call the variable inside the if/elif function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
not able to call the variable inside the if/elif function
#1
class new_():
    def __init__(self):
        self.std_=20000
      
        
    def new1 (self):
        if (0<i<=600000):
            s=10    
        elif (800001<i<=1300000):   
            s=20

i=1000000
for i in range (0,i,100000):
	data=new()
	data.new1()
	s+=s #not able to call the variable inside the if/elif function
	print(s)
#how to call the variable inside the class or function
#I want to add "s" each and every 100000 interval
buran write Feb-08-2025, 04:55 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
What are you trying to do? Your example makes no sense.

Given your code, my first suggestion is have new1 return the value of s using return s.

Or should s be an instance variable? self.s = 20 and use data.s to get the value.

Or should s be a class variable, or a global? Should new1() be a standalone function? I don't see any reference to new_ or any instances of new_.

How best to implement getting s depends a lot on what you want to do with s and what s means.
mareeswaran likes this post
Reply
#3
Lets get the function working first!

Does this function do something like what you want?

Keep the numbers small to start with for better oversight, all those zeroes make me dizzy!

def do_stuff(num):
    s = num
    t = 0
    for i in range (0,10):
        if i <= 4:
            s +=10 
        elif i > 4:
           s += 20
        t = t + s
        print(f's = {s}')
        print(f't = {t}')
    return s,t

answer = do_stuff(0) # returns  (150, 700)
I think a function usually should return some value, so put a return in there.
mareeswaran likes this post
Reply
#4
(Feb-09-2025, 08:09 AM)Pedroski55 Wrote: Lets get the function working first!

Does this function do something like what you want?

Keep the numbers small to start with for better oversight, all those zeroes make me dizzy!

def do_stuff(num):
    s = num
    t = 0
    for i in range (0,10):
        if i <= 4:
            s +=10 
        elif i > 4:
           s += 20
        t = t + s
        print(f's = {s}')
        print(f't = {t}')
    return s,t

answer = do_stuff(0) # returns  (150, 700)
I think a function usually should return some value, so put a return in there.

Understood now. thank you. I solved my issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Substitution call keys Bobbee 15 2,763 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  Variable being erased inside of if statement deusablutum 8 2,137 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Variable for the value element in the index function?? Learner1 8 2,945 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 1,302 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,612 Nov-23-2023, 02:53 PM
Last Post: rob101
  How to create a variable only for use inside the scope of a while loop? Radical 10 8,288 Nov-07-2023, 09:49 AM
Last Post: buran
  Printing the variable from defined function jws 7 7,427 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 2,173 Aug-07-2023, 05:58 PM
Last Post: Karp
  with open context inside of a recursive function billykid999 1 1,311 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 1,918 May-02-2023, 08:40 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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