Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

0 votes

In working on Program Practice 2, I want to save the results summary statement as a variable so I don't need to type it out all the time, but I know that strings are immutable and print functions aren't saved into variables. Do we know enough to do this easily?

asked in CSC201 by (213 points)

2 Answers

0 votes

While string objects themselves are immutable, variables can still store different string values over time.

Try running the following code:

adj = 'happy'
noun = 'penguin'
phrase = adj + ' ' + noun
print(phrase)

Or this:

s = "emu's are"
s = s + " great birds!"
print(s)
answered by (113 points)
0 votes

If you are talking about the strings like "Paper covers rock", "Rock smashes paper", etc., you could put each in a string literal at the top of the program like we've done for NUM_CIRCLES or MAX_RADIUS. You could have PAPER_WIN = "Paper covers rock" for example. Then your print statement would be print(PAPER_WIN).

OR

You could mean, could you have a string which stores the message, and then a print statement after the conditional statement. That would be possible too. Use the conditional statement to store whatever you want printed in a variable like message, then after the conditional statement print(message)

Of course, you could do use both of these ideas together.

answered by (4.2k points)
...