3 Replies - 454 Views - Last Post: 12 August 2012 - 06:08 PM Rate Topic: -----

#1 shulgin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 12-August 12

User generated variables in python

Posted 12 August 2012 - 05:05 PM

Hey guys,

Completely new to the forum so firstly hello to all.

I'm new to programming and am currently teaching myself python with the help of some online tutes etc.

Anyway I've decided to actually write something that may be of use to me, to put it simply it will be a simple little database (which I hope to turn into a GUI at a later date).

Basically the programme will present the user with the following


print "Welcome to DataLoger (HNMR)! V1.0\n"
peaks = raw_input("How many peaks do you you need to enter ?\n")

yorn = raw_input("so thats " + peaks + " peaks: (Y/N) ?\n")
yorn = yorn.lower()

if yorn == 'y':
    print "Lets iput data for those peaks !" 
elif yorn == 'n': 
    peaks = raw_input("How many peaks do you you need to enter ?\n")
else: 
    print "error"




so now I want to take the "peaks" variable, which will be an integer, and create a whole host of variables based on that integer. I then want some code (below) to repeat itself and save data as variables so that they can be later formated, printed and saved to a text doc.

shiftx = raw_input("What is the shift of peak No " + x + ":")
multx = raw_input("What is the multiplicity of peak No " + x + ":")
integrationx = raw_input("What is the integration of peak No " + x + ":")
jcoupx = raw_input("What is the J-coupling of peak No " + x + ":")



so if the original peaks variable is say 4, the above code will repeat 4 times, and the "x" will be 1 -4 so I'll have 4 sets of data shift1, mult1, intergration1, jcoup1......shift4 etc.

I've tried doing a while loop and use the "+" to increment the variables doing something along the lines of this, but it didn't work

x = peaks

while (x != 0)
shift + str(x) = raw_input("What is the shift of peak No " + x + ":")
mult+ str(x) = raw_input("What is the multiplicity of peak No " + x + ":")
integration+ str(x) = raw_input("What is the integration of peak No " + x + ":")
jcoup+ str(x) = raw_input("What is the J-coupling of peak No " + x + ":")
x = x - 1




I've been doing some reading and it looks like using an array is the way to go with this but I have no idea how they'll work for what I want to do (even after reading a few tutorials)

Please be aware that I'm brand new to programming and I may not understand a whole heap of jargon at this time.
And I know this isn't a place to ask for code but if anyone can fine me an example of some code that's doing something similar to what I intend to do here that'd be awesome !

Cheers

Shulgin

Is This A Good Question/Topic? 0
  • +

Replies To: User generated variables in python

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: User generated variables in python

Posted 12 August 2012 - 05:12 PM

You create an empty list (array) by writing varname = [] (so for example shift).

You append an item to a list by writing shift.append(item) (which would be the equivalent of shift + str(x) = item in your notation).

You can then access the items of the list by index by writing shift[x] (instead of shift + str(x)).

For more information see your tutorial/book's section on lists and loops (which you definitely should since this is important stuff).
Was This Post Helpful? 0
  • +
  • -

#3 shulgin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 12-August 12

Re: User generated variables in python

Posted 12 August 2012 - 05:40 PM

Cheers sepp,

does that mean that when I append the list I can do the following ?

while (x < peaks):
    shift.append(raw_input("what is the shift of peak No " + x + " ?:"))
    x = x + 1


Was This Post Helpful? 0
  • +
  • -

#4 shulgin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 12-August 12

Re: User generated variables in python

Posted 12 August 2012 - 06:08 PM

Nevermind I got it to work, thanks again !

Cheers

shulgin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1