[Tutor] Question about loop and assigning variables

Alan Gauld alan.gauld at yahoo.co.uk
Wed Apr 5 21:02:33 EDT 2017


On 05/04/17 20:07, Fazal Khan wrote:

> assign different variables with each loop

You can't, but you can fake it...

> def BeamInfo(x):
>   for Bnum in x:
>         if plan1.IonBeamSequence[Bnum].ScanMode == 'MODULATED':
>             TxTableAngle =
> plan1.IonBeamSequence[Bnum].IonControlPointSequence[0].PatientSupportAngle
>             print(TxTableAngle)
>         else:
>             None

> Ideally what I want is this: when Bnum is 0, then "TxTableAngle" will be
> appended with "_0" (eg.TxTableAngle_0). When Bnum is 1 then TxTableAngle_1,
> etc. How do I do this in python?

Use a list (or a dictionary if the values are not sequential or numeric)

So
TxTableAngle_0 -> TxTableAngle[0]
TxTableAngle_1 -> TxTableAngle[1]

It's one extra character but hopefully that won't be too painful...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list