Why is does the variable never decrease? - variables

def complex():
answer = raw_input("Would you like to run this program?")
answer = answer.lower()
money = 5
if "yes" in answer:
print money
money = money - 1
complex()
else:
quit()
complex()
For some reason, every-time I enter "yes" into the raw_input it spits out 5. But I want it to spit out 5 then when I type yes again I want it to spit out 4, and then if I type yes again I want it to spit out 3....
I fixed this by using the Global statement:
money = 5
def complex():
answer = raw_input("Would you like to run this program?")
answer = answer.lower()
if "yes" in answer:
global money
print money
money = money - 1
complex()
else:
quit()
complex()

This is what your procedure is doing :
Get user input
Set variable money to 5
Validate if user input value is "yes" and if so print value if variable money which is 5.
Set variable money to 5 - 1 = 4
Run procedure complex()
Get user input
Set variable money to 5
Validate if user input value is "yes" and if so print value if variable money which is 5.
Set variable money to 5 - 1 = 4
Run procedure complex()
...
etc.
As you can see your procedure is overwriting the desired value (4) with the value of 5 with each iteration that that is why it is not working as you desire.
What you could do is make a loop to run x number of times after money has been set to five.

money is a local variable for the complex function.
Every time you call complex function the variable is created.
Try to create the variable before the method.

Within the "complex" function, you keep resetting the money variable to 5.

Since you call complex(), the line
money = 5
keeps being called. To work around this, your recursive call should not include this setting. Alternatively, use iteration.

change money = money - one to a different variable you are using it too many times and it will take money as 5
print dollars
dollars = money - 1

Related

Print multiple data sets in a range

I've made a small program that is supposed to read data in a range input it into an object oriented program, and then return the full data set. the issue is that when I run the file it only return data on the third procedure
I tried printing other procedure sets but idk how to do that, i'm thinking this will only work if i replace the procedures from generic to specific. as in instead of Procedure name for all of them procedures 1, 2, and 3
for i in range (3):
procedure_name = ('Physical Exam')
date_of = ("Nov 6th 2022")
doctor = ('Dr. Irvine')
charge = ('$ 250.00')
procedure_name = ('X-ray')
date_of = ("Nov 6th 2022")
doctor = ('Dr. Jamison')
charge = ('$ 500.00')
procedure_name = ('Blood test')
date_of = ("Nov 6th 2022")
doctor = ('Dr. Smith')
charge = ('$ 200.00')
procedure = HW6_RODRIGUEZ_1.Procedure(procedure_name,date_of,doctor,charge)
print(f'Procedure {i+1}')
print(procedure)
print(i, end=" ")
if name == 'main':
main()
So, I think you may have misunderstood some things when it comes to variables, OOP and looping.
When you define a variable, that variable is set to the last value it is assigned. So if you have the following code:
a = 1
a = 2
a = 3
The final value of the variable 'a' will be 3, as that is the last value it is assigned.
As for loops, whatever you have written in a for loop will be repeated for a specified number of times. This means if you want to write a loop that prints "hello" 5 times, you'd write the following:
for i in range(5):
print("hello")
What your loop is essentially doing is overwriting the same 3 variables 3 times over, this won't be assigning new values to an object.
When it comes to creating an object that you assign variable to, you need to first write the code for your class. Your class can have attributes like the variables you've stated. It could look something like this:
class procedure:
def __init__(self, procedure_name, date_of, doctor, charge):
self.procedure_name = procedure_name
self.date_of = date_of
self.doctor = doctor
self.charge = charge
Now, to set up a procedure object, you just assign a variable to procedure with the desired variables as parameters, like so:
new = procedure('X-ray','Nov 6th 2022','Dr. Jamison','$ 500.00')
And to access a variable, you just need to write procedureName.attribute. For example, using the object I just set up:
print(new.doctor)
Would output 'Dr. Jamison'.
If you want to store a bunch of them, I would recommend storing them in a list or a dictionary, depending on how you want to look them up.
I hope this helps! If you are new to programming, I would recommend some simpler programs such as a program that prints the nursery rhyme 10 green bottles using loops, or maybe making a quiz.
Best of luck.

How to chose options in a while loop

My program --> I Will ask the user to introduce a number and I want to make that if the number is not in a random sequence (I choose 1,2,3) of numbers, the user need to write again a number until the number they enter is in the sequence:
a = (1,2,3)
option = int(input(''))
while option != a:
print('Enter a number between 1 and 3 !!')
option = int(input(''))
So as you can see I use the variable as a tuple but I don't know how to do it.. =(
Assuming the use of a tuple is obligatory, you will need to get input as a string, because it is iterable type. It will alow you easily convert to int, sign by sign, thru list comprehension. Now you have a list of ints, which you simply convert to a tuple. The final option variable looks:
option = tuple([int(sign) for sign in str(input(''))])
But consider keeping your signature in int instead of tuple. Int number is also unequivocal if its about sequence. In python 123 == 132 returns False. That way, you need only to replace:
a = (1,2,3)
by a:
a = 123
And script will works.

Possible to store a value in variable in SPSS?

Is is possible in SPSS to store a value in a variable (not a variable created in a data set)?
For example I have a loop for which I want to pass the value 4 to all the locations in the loop that say NumLvl.
NumLvl = 4.
VECTOR A1L(NumLvl-1).
LOOP #i = 1 to NumLvl-1.
COMPUTE A1L(#i) = 0.
IF(att1 = #i) A1L(#i) = 1.
IF(att1 = NumLvl) A1L(#i) = -1.
END LOOP.
EXECUTE.
You can do this using DEFINE / !ENDDEFINE SPSSs Macro Facility, for example:
DEFINE !MyVar () 4 !ENDDEFINE.
You can then use !MyVar as a substitute for 4 wherever in your syntax you wish.
See DEFINE / !ENDDEFINE documentation for further notes.

Looping through variables in spss

Im looking for a way to loop through variables (eg week01 to week52) and count the number of times the value changes across the them. For example
week01 to week18 may be coded as 1
week19 to week40 may be coded as 4
and week 41 to 52 may be coded as 3
That would be 2 transistions within the data.
How could i go about writing a code that can find me this information? I'm rather new to this and some help to get me in the right direction would be very appreciated.
You can use the DO REPEAT command to loop through variable lists. Below is an example of using this command to create a before date and after date to compare, and increment a count variable whenever these two variables are different.
data list fixed / observation (A1).
begin data
1
2
3
4
5
end data.
*making random data.
vector week(52).
do repeat week = week1 to week52.
compute week = RND(RV.UNIFORM(0.5,4.4)).
end repeat.
execute.
*initialize count to zero.
compute count = 0.
do repeat week_after = week2 to week52 / week_before = week1 to week51.
if week_after <> week_before count = count + 1.
end repeat.
execute.

Increment an integer

Sometimes ABAP drives me crazy with really simple tasks such as incrementing an integer within a loop...
Here's my try:
METHOD test.
DATA lv_id TYPE integer.
lv_id = 1.
LOOP AT x ASSIGNING <y>.
lv_id = lv_id+1.
ENDLOOP.
ENDMETHOD.
This results in the error message Field type "I" does not permit subfield access.
You already answered the question yourself, but to make things a bit clearer:
variable + 1
is an arithmetic expression - add 1 to the value of the variable.
variable+1
is an offset operation on a character variable. For example, if variable contains ABC, variable+1 is BC.
This can be especially confusing when dealing with NUMCs. For example, with variable = '4711', variable + 1 is evaluated to 4712, whereas variable+1 is '711' (a character sequence).
The error you saw occurred because it's not possible to perform the index operation on a non-character-like variable.
You mean like:
ADD 1 to lv_id.
By the way, when you loop over an internal table, SY-TABIX has the loop counter.
Uh, I got it.
It's the f****** spaces...
lv_id = lv_id + 1
works...
Simple
DATA : gv_inc type I .
place this statement in loop
gv_inc = gv_inc + 1 .
from SAP NetWeaver Version 7.54 you can also use:
lv_id += 1.
Instead of
lv_id = lv_id + 1.
Happy coding!
If you are going to increment every loop cycle than you can directly get the table size.
describe table x lines data(lv_id). "Out side of the loop.