Python Forum
Assigning a new variable in a IF loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning a new variable in a IF loop
#1
I am trying a very basic IF loop in which i am trying to create a new variable like below
for i in range(1,16):
   for j in range(i,i+14):
       if (j > 13):
            {
             jdisplay = "j%13"
            }
       else:
            {
             jdisplay=j
            }
print(i,j)
i am not getting this right, i want the number not to exceed 13, if it is greater than 13 a new variable called jdisplay should be created and the value should be modulo division of j with 13.
Reply
#2
(Mar-01-2018, 05:09 PM)pythoneer Wrote: a new variable called jdisplay should be created
How many different variables, all named jdisplay, do you want?
And why are there braces in your code?
Reply
#3
oops, sorry , i got this working, this is what happens when i switch from something else to something i started to learn..thanks
for i in range(1,16):
   for j in range(i,i+14):
       if (j > 13):
        jdisplay = j%13

       else:
        jdisplay=j
        print(i,jdisplay)
Reply
#4
i think you need to dedent line 8 one level.
Also note that your indentation is not consistent - you have 3, 4 and 1 space indentation.
Reply
#5
So that works, right? I'm not a huge fan of hoisting variables to their containing scope, so even though it works, I'd put a default value before the loop. Something like:
jdisplay = None
for i in range(1, 16):
    for j in range(i, i+14):
        if j > 13:
            jdisplay = j % 13
        else:
            jdisplay = j
print(jdisplay)
Reply
#6
(Mar-01-2018, 06:19 PM)buran Wrote: i think you need to dedent line 8 one level.
Also note that your indentation is not consistent - you have 3, 4 and 1 space indentation.

Thanks for the inputs, will correct it

(Mar-01-2018, 07:42 PM)nilamo Wrote: So that works, right? I'm not a huge fan of hoisting variables to their containing scope, so even though it works, I'd put a default value before the loop. Something like:
jdisplay = None
for i in range(1, 16):
    for j in range(i, i+14):
        if j > 13:
            jdisplay = j % 13
        else:
            jdisplay = j
print(jdisplay)

Curious to know the difference it would make. Anyways the old one works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,101 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,129 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,109 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,410 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Assigning a new value to variable uriel 1 1,568 Dec-04-2021, 02:59 PM
Last Post: Underscore
  Using Excel Cell As A Variable In A Loop knight2000 7 4,013 Aug-25-2021, 12:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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