Bugofcake
Bugofcake

Reputation: 29

How do I print a string output from another string (clear example)

i'm trying to make a program for battleships but i'm having an issue with the following program (simplified version)

import random
A = "the one"
B = "who"
C = "lost"
letters = ['A', 'B', 'C']
D = (random.choice(letters))
print(str(D))

It only outputs A B or C but I want it to output the strings content not the name of the string eg I want it to print 'the one' 'who' or 'lost' instead of

Upvotes: 0

Views: 35

Answers (2)

hspandher
hspandher

Reputation: 16743

You need to use variables then, not strings.

letters = [A, B, C]

When you enclose variable names with quotes, it creates, well, strings.

Upvotes: 1

cs95
cs95

Reputation: 402603

Just remove the single quotes so you create a list holding string variables instead of a list holding strings.

In [5]: letters = [A, B, C]

In [6]: random.choice(letters)
Out[6]: 'lost'

Upvotes: 0

Related Questions