LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2006, 09:55 AM   #1
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Rep: Reputation: 15
Get strings from list using variables


Hi all, I'm struggling to get my list items using variables, here is what I have...

Code:
  def getmyname(self):
    myint = self.var.get()
    users = self.getUsers()
    if myint:
      myname = users[myint] + '/'
    else:
      showwarning('No User', 'No user has been selected')
      myname = ''
    return myname
and it doesn't like that.

Quote:
myname = users[myint] + '/'
TypeError: list indices must be integers
self.var.get() just gets the variable from the radio button, if 0 then myname is the name of the first user in my list etc. self.getUsers() is a sub procedure that returns all names in list form from a text file. It's simple enough but I can't work out how to get the list to accept variables. Any ideas anyone? Thanks.

Edit: This is python coding.
 
Old 10-27-2006, 10:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by mwade
Hi all, I'm struggling to get my list items using variables, here is what I have...

Code:
  def getmyname(self):
    myint = self.var.get()
    users = self.getUsers()
    if myint:
      myname = users[myint] + '/'
    else:
      showwarning('No User', 'No user has been selected')
      myname = ''
    return myname
and it doesn't like that.



self.var.get() just gets the variable from the radio button, if 0 then myname is the name of the first user in my list etc. self.getUsers() is a sub procedure that returns all names in list form from a text file. It's simple enough but I can't work out how to get the list to accept variables. Any ideas anyone? Thanks.

Edit: This is python coding.
Judging from the error message, your myint is not an integer.
Try converting it to int before using it with the "users" list
eg
Code:
..
myint = int(self.var.get())
...

Last edited by ghostdog74; 10-27-2006 at 10:37 AM.
 
Old 10-28-2006, 06:26 AM   #3
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
I thought that would work until this error appeared What does this mean? I've searched the internet and there are plenty of things about it but none have helped me. Am I approaching this the correct way or is there an easier way to link radio buttons to a list of users? Thanks.

Code:
    myint = int(self.var.get())
ValueError: invalid literal for int():
 
Old 10-28-2006, 08:14 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
hi
how about trying:
Code:
...
myint = self.var.get()
myint = int(myint)
..
 
Old 10-28-2006, 11:09 AM   #5
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
Thumbs up

That's it! A mix between me being an idiot and python not liking some coding caused the error. I was not clicking a user and the error handling was in the wrong place. Here is the working code.

Code:
  def getmyname(self):
    myint = self.var.get()
    users = self.getUsers()
    if myint:
      myint = int(myint)
      myname = users[myint] + '/'
    else:
      showwarning('No User', 'No user has been selected')
      myname = ''
    return myname
It wouldn't work with the
Code:
myint = int(myint)
line above the
Code:
if myint:
line because it can't turn nothing into an integer. Well problem solved now so thanks alot ghostdog74!
 
Old 10-28-2006, 08:01 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
no problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: Ruby and Writing Variables In Strings lmcilwain Programming 4 10-18-2006 01:19 PM
(bash) how to list all variables? Duudson Programming 11 03-24-2005 12:35 PM
C++ converting strings to variables dhbiker Programming 9 05-31-2004 04:06 AM
How can I get the full list of environment variables (RedHat 7.2)? yuzuohong Linux - General 1 06-07-2003 03:28 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM


All times are GMT -5. The time now is 08:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration