Issue with my code
Hi, I'm trying to create this program that counts the occurrences of each digit in a string which the user have to enter. Here is my code: s=input("Enter a string, eg(4856w23874): ") s=list(s) checkS=['0','1','2','3','4','5','6','7','8','9'] for i in s: if i in checkS: t=s.count(i) if t>1: for k in range(1,t): s=s.remove(i) print(i, "occurs", t,"times.") elif t==1: print(i,"occurs 1 time.") else: pass but it keeps showing this error: t=s.count(i) AttributeError: 'NoneType' object has no attribute 'count' I wanted to show like this: Example: Enter a string: 3233456 3 occurs 3 2 occurs 1 4 occurs 1 5 occurs 1 6 occurs 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Issue with my code
Also I’m using Python 3.2.3. On Tuesday, February 5, 2013 1:38:55 PM UTC-5, maiden129 wrote: > Hi, > > > > I'm trying to create this program that counts the occurrences of each digit > in a string which the user have to enter. > > > > Here is my code: > > > > s=input("Enter a string, eg(4856w23874): ") > > s=list(s) > > > > checkS=['0','1','2','3','4','5','6','7','8','9'] > > > > for i in s: > > if i in checkS: > > t=s.count(i) > > if t>1: > > for k in range(1,t): > > s=s.remove(i) > > print(i, "occurs", t,"times.") > > > > elif t==1: > > print(i,"occurs 1 time.") > > else: pass > > > > but it keeps showing this error: > > > > t=s.count(i) > > AttributeError: 'NoneType' object has no attribute 'count' > > > > I wanted to show like this: > > > > Example: > > > > Enter a string: 3233456 > > > > 3 occurs 3 > > 2 occurs 1 > > 4 occurs 1 > > 5 occurs 1 > > 6 occurs 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Issue with my code
On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote: > On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote: > > > Hi, > > > > > > I'm trying to create this program that counts the occurrences of each > > > digit in a string which the user have to enter. > > > > > > Here is my code: > > > > > > s=input("Enter a string, eg(4856w23874): ") > > > s=list(s) > > > > > > checkS=['0','1','2','3','4','5','6','7','8','9'] > > > > > > for i in s: > > > if i in checkS: > > > t=s.count(i) > > > if t>1: > > > for k in range(1,t): > > > s=s.remove(i) > > > print(i, "occurs", t,"times.") > > > > > > elif t==1: > > > print(i,"occurs 1 time.") > > > else: pass > > > > > > but it keeps showing this error: > > > > > > t=s.count(i) > > > AttributeError: 'NoneType' object has no attribute 'count' > > > > s=s.remove(i) does not return a new list but modifies the list in > > place. > > > > So you probably just want > > > > >>> s.remove(i) > > > > Also, there are various inefficiencies in your code, but that is the > > main issue with the AttributeError. when I removed "s.remove(i), it starts to repeat the number of occurrences too many times like this: 2 occurs 3 times. 2 occurs 3 times. 3 occurs 3 times. 3 occurs 3 times. 2 occurs 3 times. 2 occurs 3 times. 5 occurs 1 time. 3 occurs 3 times. 3 occurs 3 times. 4 occurs 1 time. 3 occurs 3 times. 3 occurs 3 times. 1 occurs 1 time. 2 occurs 3 times. 2 occurs 3 times. How can I stop this? -- http://mail.python.org/mailman/listinfo/python-list
Re: Issue with my code
How to reverse the two loops? On Tuesday, February 5, 2013 2:43:47 PM UTC-5, Dave Angel wrote: > On 02/05/2013 02:20 PM, maiden129 wrote: > > > On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote: > > >> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote: > > >> > > > > > > > > when I removed "s.remove(i), it starts to repeat the number of occurrences > > too > > > > > > many times like this: > > > > > > 2 occurs 3 times. > > > 2 occurs 3 times. > > > 3 occurs 3 times. > > > 3 occurs 3 times. > > > 2 occurs 3 times. > > > 2 occurs 3 times. > > > 5 occurs 1 time. > > > 3 occurs 3 times. > > > 3 occurs 3 times. > > > 4 occurs 1 time. > > > 3 occurs 3 times. > > > 3 occurs 3 times. > > > 1 occurs 1 time. > > > 2 occurs 3 times. > > > 2 occurs 3 times. > > > > > > How can I stop this? > > > > > > > As MRAB pointed out, don't delete items from a list you're iterating > > over. It can make the iterator go nuts. He suggests the collections > > module. > > > > But if you want to do it by hand, one approach is to reverse the two > > loops. Iterate over the characters in CheckS list, examining the entire > > s list for each one and figuring out how many times the character occurs. > > > > Another approach is to build a dict, or a defaultdict, to keep counts > > for each of the characters in CheckS. > > > > -- > > DaveA -- http://mail.python.org/mailman/listinfo/python-list
Struggling with program
I'm trying to do this assignment and it not working, I don't understand why... This is what I have to do: Write the definition of a class Player containing: An instance variable name of type String , initialized to the empty String. An instance variable score of type int , initialized to zero. A method called set_name that has one parameter, whose value it assigns to the instance variable name . A method called set_score that has one parameter, whose value it assigns to the instance variable score . A method called get_name that has no parameters and that returns the value of the instance variable name . A method called get_score that has no parameters and that returns the value of the instance variable score . No constructor need be defined. Here is my code: class Player: name = '' def __init__(self,score = 0) def set_name (self): self.name def set_score (self): self.score def get_name return name def get_score return score can someone please help me? -- http://mail.python.org/mailman/listinfo/python-list
Python GUI questions
Hello, I'm using python 3.2.3 and I'm making a program that show the of occurrences of the character in the string in Tkinter. My questions are: How can I make an empty Entry object that will hold a word that a user will enter? How to make an empty Entry object that will hold a single character that the user will enter? How to A Button object with a text equal to "Count"? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python GUI questions
On Tuesday, March 19, 2013 5:39:51 PM UTC-4, Chris Angelico wrote: > On Wed, Mar 20, 2013 at 6:01 AM, maiden129 wrote: > > > Hello, > > > > > > I'm using python 3.2.3 and I'm making a program that show the of > > occurrences of the character in the string in Tkinter. > > > > This sounds like homework. Have you had a try at it yourself before > > asking? If so, show us your code, and point out where the problem is; > > if not, give it your best effort before you try to get someone else to > > do it for you, as that's the only way to learn! > > > > ChrisA This is not homework, I'm trying to learn how do I create a empty object. That was my main question. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python GUI questions
On Tuesday, March 19, 2013 8:57:42 PM UTC-4, Rick Johnson wrote: > On Tuesday, March 19, 2013 2:01:24 PM UTC-5, maiden129 wrote: > > > Hello, > > > > > > I'm using python 3.2.3 and I'm making a program that show > > > the of occurrences of the character in the string in > > > Tkinter. > > > > > > My questions are: > > > > > > How can I make an empty Entry object that will hold a word > > > that a user will enter? > > > > I believe you meant to say: "How can i CREATE an entry field to accommodate > user input?" > > > > Easy. > > > > > How to make an empty Entry object that will hold a single > > > character that the user will enter? > > > > Not as easy, but still quite doable. Do you want to filter the input, > allowing only a single character? > > > > > How to A Button object with a text equal to "Count"? > > > > Easy-pee-see. Follow this yellow brick road to enlightenment. > > > > http://effbot.org/tkinterbook/tkinter-whats-tkinter.htm Hello, Here is my try to answer some of questions: from tkinter import * class word: def __init__(self,Entry,Character): window = Tk() window.title("Widget") top = Tk() L1 = Label(top, text="Enter a string") L1.pack( side = LEFT) E1 = Entry(top, bd =5) E1.pack(side = RIGHT) top.mainloop() L2 = Label(bottom, text="Number of single characters") L2.pack( side = LEFT) E2 = Entry(bottom, bd =5) button = Tkinter.Button(bottom, text ="Count", command = countCharacter).pack() def countChacater(self): count = word.count(character) I'm just struggling with only how to create an object that will hold a single character that the user will enter. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python GUI questions
On Tuesday, March 19, 2013 10:16:25 PM UTC-4, Rick Johnson wrote: > On Mar 19, 8:25 pm, maiden129 wrote: So should I redo my other code that I created with the radioButtons to change the colors of a text? from tkinter import * class buttons: def __init__(self): window = Tk() window.title("Radio buttons and buttons") self.var = IntVar() w1 = Radiobutton(window, text="Red", variable=self.var, value=1,command=changeColor).pack() w2 = Radiobutton(window, text="Yellow", variable=self.var, value=2,command=changeColor).pack() w3 = Radiobutton(window, text="White", variable=self.var, value=3,command=changeColor).pack() w4 = Radiobutton(window, text="Grey", variable=self.var, value=4,command=changeColor).pack() w5 = Radiobutton(window, text="Green", variable=self.var, value=5,command=changeColor).pack() B1 = Button ( window, text="<=", command=LEFT).pack() B2 = Button ( window, text="=>", command=RIGHT).pack() def changeColor(self): self.var.get() window.mainloop() buttons() -- http://mail.python.org/mailman/listinfo/python-list
Global NameError Fix?
Hello, I'm using the version 3.2.3 of Python and I am having an issue in my program and I don't know how to fix it: counterLabel["text"] = str(counter) NameError: global name 'counterLabel' is not defined Here is my program: from tkinter import * class CounterButton(Button): def __init__(self, window): super(CounterButton,self).__init__(window, text = "0", command=self.startCounter) def startCounter(self): counter = int(self["text"]) counter +=1 counterLabel["text"] = str(counter) window = Tk() window.title("counter") counterButton1 = CounterButton(window) counterButton2 = CounterButton(window) counterButton1.pack() counterButton1.pack() window.mainloop() -- http://mail.python.org/mailman/listinfo/python-list