Jump to content

Python Add to Object Attribute

LtStaffel
Go to solution Solved by fizzlesticks,
1 minute ago, LtStaffel said:

Sure, here:

The line

char

is never going to raise an exception since you define it as the loop variable. Which means you never create an instance of your class and inside the else clause 'char' is type 'str' not 'chars' like you expect. Strings have a built-in count function so doing 'char.count += 1' is trying to add a built-in function and an int just like error says. 

Hello,

 

Simple question: How can I add 1 to the already integer value attribute of my object in Python? (In essence execute object.attribute += 1)

 

Thanks in advance!

-LtStaffel

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, fizzlesticks said:

What's wrong with object.attribute += 1?

 

Traceback (most recent call last):
  File "I:/Assorted/Small Programs/caesarcracker.py", line 16, in <module>
    char.count += 1
TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'

 

Get a type error because it wants to call char.count a builtin function instead of operating on what it returns, which is weird so I tried temp = char.count then on another line char.count = temp+1 but that didn't work either.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, LtStaffel said:

Traceback (most recent call last):
  File "I:/Assorted/Small Programs/caesarcracker.py", line 16, in <module>
    char.count += 1
TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'

What type is 'char'?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, fizzlesticks said:

What type is 'char'?

my object, sorry for the confusing name

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, LtStaffel said:

Get a type error because it wants to call char.count a builtin function instead of operating on what it returns,

So char.count is a function? And you want to add 1 to what char.count() returns?

Would be a lot easier to help if you posted your code.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

So char.count is a function? And you want to add 1 to what char.count() returns?

Would be a lot easier to help if you posted your code.

Sure, here:

raw = raw_input("Please enter the ciphered string to crack: ")

class chars:
    def __init__(self, character):
        self.character = character
        self.count = 1

for char in raw:
    try:
        char
    except NameError:
        char = chars(char.lower())
    else:
        char.count += 1

I'm just trying to count the characters in raw.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, LtStaffel said:

Sure, here:


raw = raw_input("Please enter the ciphered string to crack: ")

class chars:
    def __init__(self, character):
        self.character = character
        self.count = 1

for char in raw:
    try:
        char
    except NameError:
        char = chars(char.lower())
    else:
        char.count += 1

I'm just trying to count the characters in raw.

As in, you're just trying to count the number of times each character appears (i.e., for frequency analysis)?  If so, a dictionary is your best bet:

# Using list comprehension to make the dictionary
raw = raw_input("Please enter the ciphered string to crack: ")
freqs = {i:raw.count(i) for i in set(raw)}

# Or, iterating through the string
raw = raw_input("Please enter the ciphered string to crack: ")
freqs = {}
for i in raw:
  try
    freqs[i] += 1
  except KeyError:
    # character not in dictionary's keys, initialize count to 1
    freqs[i] = 1

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LtStaffel said:

Sure, here:

The line

char

is never going to raise an exception since you define it as the loop variable. Which means you never create an instance of your class and inside the else clause 'char' is type 'str' not 'chars' like you expect. Strings have a built-in count function so doing 'char.count += 1' is trying to add a built-in function and an int just like error says. 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

Counter would be better.

I always forget about everything other than defaultdicts in the collections module.  A counter is certainly better if the goal is frequency analysis, yes--more readable than list comprehension (but so much less fun...), and it has that nice .most_common() method.

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Azgoth 2 said:

As in, you're just trying to count the number of times each character appears (i.e., for frequency analysis)?  If so, a dictionary is your best bet:

I tried a dictionary (succesfully) but by their nature they are not ordered which is really needed for how I'm using their frequencies.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, LtStaffel said:

I tried a dictionary (succesfully) but by their nature they are not ordered which is really needed for how I'm using their frequencies.

See @fizzlesticks reply above; try using a counter and the .most_common method (which returns items sorted by frequency).

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Azgoth 2 said:

See @fizzlesticks

Yep, I'm looking at the counter stuff he(?) linked to.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LtStaffel said:

I tried a dictionary but by their nature they are not ordered.

OrderedDict is. You could also make an OrderedCounter with just a bit of multi inheritance. 

 

class OrderedCounter(Counter, OrderedDict):
    pass

 

1474412270.2748842

Link to comment
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

×