Home > Enterprise >  Three user inputs three global variables
Three user inputs three global variables

Time:06-17

I’m having problems with changing my global variables within my function, which also uses user input and (too many) if statements to do so. I keep getting the same error but at different points if I change something.

m=0.0
AH=1.0
ndays=0.0
Day_Count = (ndays," day out of 90")
def AtomicHabits():

   wo = input("Did you work out? Y/N? ")

   tr = input("Did you study/practice trading? Y/N?")

   co = input("Practice coding? Y/N?")

   if (wo=='y'):
    m=m 1
   if (tr=='y'):
    m=m 1
   if (co=='y'):
    m=m 1
   if (m==3):
    m=.01
   elif (m==2):
    m=.0066
   elif (m==1):
    m=.0033
   elif (m==0):
    m=(-.01)

   AH=AH*(1.0 m)
   ndays=ndays 1
   return AH, m, ndays; 

AtomicHabits()

print(“Today’s change is “, m*100, “% on ”, Day_count, bringing total improvement to “, AH )

I’m completely lost, first time posting here so idek how to use the search

CodePudding user response:

You need to assign them as global inside your function.

m = 0
...

def func():
    global m
    .. 
    m = 1

This will make m = 1

CodePudding user response:

I would rewrite your code to something along this lines.

  • Split input part
  • Modularize the code to have build extensible testing on top it.
M = 0.0
AH = 1.0
Ndays=0.0

def check_atomic_habits(wo,tr,co):
  global M,AH,Ndays
  
  if (wo=='y'):
    M=M 1
  if (tr=='y'):
    M=M 1
  if (co=='y'):
    M=M 1
  if (M==3):
    M=.01
  elif (M==2):
    M=.0066
  elif (M==1):
    M=.0033
  elif (M==0):
    M-=-.01
  
  AH=AH*(1.0 M)
  Ndays=Ndays 1
  

def user_input():
  # Replace here
  wo = "Y" # input("Did you work out? Y/N? ")
  tr = "Y" # input("Did you study/practice trading? Y/N?")
  co = "N" # input("Practice coding? Y/N?")
  return wo,tr,co

# This is simulation for two days.
for i in range(2):
  wo,tr,co=user_input()
  check_atomic_habits(wo=wo,tr=tr,co=co)
  print(f"Today’s change is , {M*100} on {Ndays} day out of 90, bringing total improvement to {AH}" )

This is would generate the following o/p:

Today’s change is , 1.0 on 1.0 day out of 90, bringing total improvement to 1.01
Today’s change is , 1.0 on 2.0 day out of 90, bringing total improvement to 1.0201

CodePudding user response:

To solve your problem you could use global variables or pass your variables as parameters of your function and return them

  • Global variables

def AtomicHabits():

    global AH, m, ndays
    
    wo = input("Did you work out? Y/N? ")
    
    tr = input("Did you study/practice trading? Y/N?")
    
    co = input("Practice coding? Y/N?")
    
    if (wo=='y'):
        m=m 1
    if (tr=='y'):
        m=m 1
    if (co=='y'):
        m=m 1
    if (m==3):
        m=.01
    elif (m==2):
        m=.0066
    elif (m==1):
        m=.0033
    elif (m==0):
        m=(-.01)
    
    AH=AH*(1.0 m)
    ndays=ndays 1

AtomicHabits()
  • parameters of your function

def AtomicHabits(AH, m, ndays):

wo = input("Did you work out? Y/N? ")
    
    tr = input("Did you study/practice trading? Y/N?")
    
    co = input("Practice coding? Y/N?")
    
    if (wo=='y'):
        m=m 1
    if (tr=='y'):
        m=m 1
    if (co=='y'):
        m=m 1
    if (m==3):
        m=.01
    elif (m==2):
        m=.0066
    elif (m==1):
        m=.0033
    elif (m==0):
        m=(-.01)
    
    AH=AH*(1.0 m)
    ndays=ndays 1
    return AH, m, ndays 

AH, m, ndays = AtomicHabits(AH, m, ndays)

I rewrote your code with a couple of changes

m = 0.0
AH = 1.0
ndays = 0.0

def AtomicHabits(m, AH, ndays):

    wo = input("Did you work out? Y/N? ")

    tr = input("Did you study/practice trading? Y/N?")

    co = input("Practice coding? Y/N?")

    m  = [wo, tr, co].count("y")
      
    if m==0:
        m = -0.01
    else:
        m = 0.01/(3/m)
    
    AH *= 1.0   m
    ndays=ndays 1
   
    return m, AH, ndays 

m, AH, ndays = AtomicHabits(m, AH, ndays)

print(f"Today’s change is , {m*100} on {ndays} day out of 90, bringing total improvement to {AH}" )
  • Related