Jump to content

Hi all

 

Here is my code

for i in range(0, 5):
  if i == 3:
    # here i want to change i to another number
    print(i)
  else:
    print(i)

I know it sounds useless but this is as simple as I could get it for a bigger program

 

How can I change the var i in the for loop?

 

If I type on line 3

i = 5

it will say that i is undefined because it is only a local variable

 

So how can I change the var i in the for loop?

Edited by Hugs12343
Error in code

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/
Share on other sites

Link to post
Share on other sites

4 minutes ago, Hugs12343 said:

 

Just define "i" before the for loop? like i = 0?

I edit my posts a lot, Twitter is @LordStreetguru just don't ask PC questions there mostly...
 

Spoiler

 

What is your budget/country for your new PC?

 

what monitor resolution/refresh rate?

 

What games or other software do you need to run?

 

 

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154934
Share on other sites

Link to post
Share on other sites

Just now, Streetguru said:

Just define "i" before the for loop? like i = 0?

Tried that doesn't work

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154937
Share on other sites

Link to post
Share on other sites

I'm not sure what you are doing, but that code should work if you fix the missing semicolon at the end of the first line.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154940
Share on other sites

Link to post
Share on other sites

Just now, tikker said:

missing semicolon at the end of the first line.

There's no semicolon. It's python

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154945
Share on other sites

Link to post
Share on other sites

1 minute ago, Hugs12343 said:

There's no semicolon. It's python

Err, colon I meant:

 

for i in range(0, 5):
  if i == 3:
    # here i want to change i to another number
    i = 5
    print(i)
  else:
    print(i)

Works just fine. Are you by chance trying to access i somewhere else? The exact error message would be useful.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154949
Share on other sites

Link to post
Share on other sites

5 minutes ago, tikker said:

Err, colon I meant:

 


for i in range(0, 5):
  if i == 3:
    # here i want to change i to another number
    i = 5
    print(i)
  else:
    print(i)

Works just fine.

can confirm.

 

EDIT: however, the value of i does not carry over outside the if statement

EDIT/EDIT: thats the purpose of a for loop isnt it? disregard..

 

what this does:

first round i = 0 => else, prints 0

second round i=1 => else, prints 1

third round, i=2 => else, prints 2

fourth round, i=3 => if, prints 5

fifth round, i=4 => prints 4

loop done.

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11154951
Share on other sites

Link to post
Share on other sites

49 minutes ago, Hugs12343 said:

Hi all

 

Here is my code


for i in range(0, 5):
  if i == 3:
    # here i want to change i to another number
    print(i)
  else:
    print(i)

I know it sounds useless but this is as simple as I could get it for a bigger program

 

How can I change the var i in the for loop?

 

If I type on line 3


i = 5

it will say that i is undefined because it is only a local variable

 

So how can I change the var i in the for loop?

 

class u():
    i = range(0,5)
for i in u.i:
    if(i == 3):
        i = 7
        print(i)
    else:
        print(i)

 

This works it and all you had to do is put variable i in a class to make it where you could change it.

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11155070
Share on other sites

Link to post
Share on other sites

3 hours ago, Hotseff said:

 


class u():
    i = range(0,5)
for i in u.i:
    if(i == 3):
        i = 7
        print(i)
    else:
        print(i)

 

This works it and all you had to do is put variable i in a class to make it where you could change it.

I tried this. but it doesn't change the variable i in the for loop.

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11155647
Share on other sites

Link to post
Share on other sites

2 hours ago, Hugs12343 said:

I tried this. but it doesn't change the variable i in the for loop.

Try this:

 

 

6 hours ago, tikker said:

Err, colon I meant:

 


for i in range(0, 5):
  if i == 3:
    # here i want to change i to another number
    i = 5
    print(i)
  else:
    print(i)

Works just fine. Are you by chance trying to access i somewhere else? The exact error message would be useful.

 

 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11155859
Share on other sites

Link to post
Share on other sites

9 hours ago, Hugs12343 said:

I know it sounds useless but this is as simple as I could get it for a bigger program

What is the bigger problem maybe we can help with that.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11156094
Share on other sites

Link to post
Share on other sites

14 hours ago, Hugs12343 said:

I tried this. but it doesn't change the variable i in the for loop.

What is it that you are trying to change about it?

Let me elaborate on my earlier snippet. I've removed the redundant double print statemement. Take a look at this example (code below as well): https://repl.it/repls/BleakGrizzledPolygon

#Show the values that will be iterated over. list(...) is needed since in Python3 range is a generator, like  xrange in Python2.
i = list(range(0, 5))
print(i)

# Loop over them.
for i in range(0, 5):
  print('Iteration ', i)
  if i == 3:
    # Change i to hold the value 5 temporarily.
    i = 5
    print('The value of i will now be', i, 'for the rest of the loop.')
  print('The value of i is', i, '\n')

 

The range function will, as its name implies, generate a range of numbers between a given start and end. By definition the value i will take the next value in the range at the start of each iteration. Once you assign a new value to it, it will keep that value for the rest of the loop, or until it is changed again. Once the next iteration starts however, i will be (re)set to its intended value: the next one in the range.

 

9 hours ago, vorticalbox said:

What is the bigger problem maybe we can help with that.

This may help us give more concrete help.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/907537-python-for-loop-help/#findComment-11157418
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×