0

Possible Duplicate:
String and integer concatenation

I created the list with range numbers from -90 to 90. Now I need to make the random number from this list to appear in my question. Can anyone help me with that? This is where I got so far:

latitude = [n for n in range(-90,90)]
record = latitude[random.randrange(-90,90)]
question =['lati','country']
questions = random.choice(question)

if questions == 'lati':
    resp = raw_input('Is Wroclaw north of ' + record)

When I tried to run this I received an error saying that i cannot concatenate 'str' and 'int' objects.

Community
  • 1
  • 1

3 Answers3

3

You cannot concatenate a string and a number. The best way to display it would be to use a format string, as follows:

resp = raw_input('Is Wroclaw north of %d' % record)
Steve Mayne
  • 21,307
  • 4
  • 47
  • 48
1

you need to use str() here, because record is an integer, so you should convert it to string first before concatenation:

resp = raw_input('Is Wroclaw north of ' + str(record))

or use string formatting:

raw_input('Is Wroclaw north of {0}'.format(record))
Ashwini Chaudhary
  • 232,417
  • 55
  • 437
  • 487
0

Use record = random.randrange(-90,90) without a list. Or the next construstion if you really need a list.

random.choice([1,2,3])
SWAPYAutomation
  • 705
  • 4
  • 11