11 Replies - 5265 Views - Last Post: 29 July 2012 - 02:19 PM Rate Topic: -----

#1 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Can't concatenate 'str' and 'int' objects

Posted 28 July 2012 - 12:39 PM

Just for fun, I'm making a 6 character password generator in Portable Python 2.7.3.

print "This is a 6 character password generator. Use wisely!"
import random
import string
letters = list(string.letters)
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
a = random.choice(letters)
b = random.choice(numbers)
c = random.choice(numbers)
d = random.choice(numbers)
e = random.choice(numbers)
f = random.choice(letters)
g = (a + b + c + d + e + f)
print g
print "Press <enter> to exit."


Every time I try to use it, it gives me the exception "
Traceback (most recent call last):
File "F:\Portable Python\Projects\password.py", line 12, in <module>
g = (a + b + c + d + e + f)
TypeError: cannot concatenate 'str' and 'int' objects"

Please give me an answer of why this is happening. If this is stupid, blame it on that im very new to python.

Is This A Good Question/Topic? 0
  • +

Replies To: Can't concatenate 'str' and 'int' objects

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Can't concatenate 'str' and 'int' objects

Posted 28 July 2012 - 01:43 PM

It's happening because you are trying to "add", String (str) and Integer (int) objects together, like it says.

A solution would be to cast all the integers to strings before you add them together, with the str() function:
g = (a + str(B)/> + str(c) + str(d) + str(e) + f)


Notice I don't cast "a" and "f", they are already strings.

Or, for each of the selection cast the choice:

b = str(random.choice(numbers))



If the numbers aren't going to be used in any calculation, just as password characters, it would be preferable to just store the integers as strings, and no casting will be necessary:
print "This is a 6 character password generator. Use wisely!"
import random
import string
letters = list(string.letters)
# They are strings here now
numbers = ("1", "2", "3", "4", "5", "6", "7", "8", "9")
a = random.choice(letters)
b = random.choice(numbers)
c = random.choice(numbers)
d = random.choice(numbers)
e = random.choice(numbers)
f = random.choice(letters)
# Now this will work just fine
g = (a + b + c + d + e + f)
print g
print "Press <enter> to exit."


Was This Post Helpful? 2
  • +
  • -

#3 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 28 July 2012 - 03:14 PM

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Can't concatenate 'str' and 'int' objects

Posted 28 July 2012 - 03:56 PM

If you don't mind a little input about your code, I'd like to give you a few suggestions. If you're not interested, just don't hit the spoiler button :)

Spoiler

This post has been edited by atraub: 28 July 2012 - 04:03 PM
Reason for edit:: I really want to emphasize that I don't have confidence in my shoddy math skills

Was This Post Helpful? 1
  • +
  • -

#5 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 28 July 2012 - 07:29 PM

Haha, I suddenly feel so un-skilled. While i never thought of that, I AM trying to think of a way it could generate its own formats, for instance, instead of us putting in "a + b + c + d + e + f", it automatically randomizes its own format. Don't know how to do that though. But your way is pretty amazing!

Then again, I suppose your way did that already, didn't it? And, would that include characters such as ",.!/"?
Was This Post Helpful? 0
  • +
  • -

#6 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 09:36 AM

Ok, found something a little odd with my current code. Occasionally, when I use the program it gives me a password "0". just zero, nothing else. not sure why this is happening, because there are no error messages. Here is my current code:
print "This is a 6 character password generator. Use wisely!"
import random
import string
letters = list(string.letters)
numbers = ('1', '2', '3', '4', '5', '6', '7', '8', '9')
a = random.choice(letters)
b = random.choice(numbers)
c = random.choice(numbers)
d = random.choice(numbers)
e = random.choice(numbers)
f = random.choice(letters)
g = a + b + c + d + e + f
h = b + c + a + f + d + e
i = b + c + a + d + f + e
j = b + a + d + f + c + e
k = b + c + d + e + a + f
l = a + f + c + b + d + e
m = b + a + f + c + d + e
n = b + c + d + a + f + e
o = b + a + c + d + f + e
p = a + b + d + f + c + e
q = a + b + f + c + d + e
for i in range(1):
    print random.choice([g, h, i, j, k, l, m, n, o, p, q])
    print ""
print "Press <enter> to exit."


Just thought you guys could figure out why this was happening.
Was This Post Helpful? 0
  • +
  • -

#7 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 12:33 PM

Hehe... whoops. From now on, please view the
print "Press <enter> to exit."
as
raw_input("Press <enter> to exit.")

Was This Post Helpful? 0
  • +
  • -

#8 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 12:43 PM

Also, please test this program in something other than IDLE, as it gives the password some screwy character.

(If it seems I'm posting too much about this, its because I'm not working on anything else right now.)
Was This Post Helpful? 0
  • +
  • -

#9 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 01:47 PM

The program gives me strange characters and zero result in IDLE only, which I don't think is a major problem - it likes to be quirky with some things. Just ignore it.

The repetition of adding permutations of letters could be simplified a lot. Consider now, making a random choice from the pool of letters or numbers you have available, and then removing said letter - that way you'll get all of them in a random order!

Also for i in range(1) is totally unnecessary, you run it 1 time, there isn't a loop really.

P.S. It's not a requirement but a standard that you import everything before writing the rest of the code, I would move the first print statement below the two imports

Anyway, I'd go for something like this, based on your one:

import random
import string
print "This is a 6 character password generator. Use wisely!"
letters = list(string.letters)
numbers = ('1', '2', '3', '4', '5', '6', '7', '8', '9')
# Empty list
chars = []
# Two numbers
for i in range(2):
    chars.append(random.choice(numbers))
# Four letters
for j in range (4):
    chars.append(random.choice(letters))

# Now "chars" is a list of 2 random numbers and 4 random letters, as before. The order doesn't matter
# Because we'll be randomly selecting them again in a minute

# Empty password
password = ""
# All of the characters (0 to the length of chars)
for k in range(len(chars)):
    # select a random element from the list of chars
    x = random.choice(chars)
    # Add the character to the password
    password = password + x
    # Now remove it so it can't be selected again
    chars.remove(x)

print password + "\n"
raw_input("Press <enter> to exit.")


This code will produce a password of 4 letters and 2 numbers in any possible order, given they are randomly selected, without manually swapping them around :)

Surely there's some clever tricks with syntax to make the code a bit more concise, but I hope you can understand and see my method in the code I have provided, given you are very new to Python.

This post has been edited by Simown: 29 July 2012 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

#10 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 02:03 PM

I get it, but i have 2 questions: why k in "for k in range", and what does \n stand for?
Was This Post Helpful? 0
  • +
  • -

#11 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 02:14 PM

For k in range(10) for example means, for k = 0 to k = 9 (which is 10 in total). It's an instruction to loop 10 times. For k in range(len(chars)) means loops from 0 to the length of the password characters. In this case it will reduce to: for k in range(6).

Do:
for k in range(100):
    print k,     


To see, it will output 0-99. "k" is a variable, so you could use "i" or "j" which I have done. Or "number" or "position" or "some-nonsense-word". It's all the same in this case.

"\n" is a special new line character to Python. Exactly the same as using print "" but without writing another print statement on the next line.

This post has been edited by Simown: 29 July 2012 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#12 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: Can't concatenate 'str' and 'int' objects

Posted 29 July 2012 - 02:19 PM

Cool, I'll try experimenting with this. Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1