-1

So I have to write a code while assuming that the grader defines the following variables

a to be some int
b to be some float
s to be some string

and then I have to write a program that prints out exactly the following, where the values in < .... > are replaced by the actual value inside the variables. However, you can only use ONE print statement.

// The variable 'a' has value of <a>. //
\\ While the variable 'b' has value of <b>. \\
// Lastly, the variable 's' has value of "<s>". //

Example, if a=1, b=1.5 and s='hi', then the expected output is:

// The variable 'a' has value of 1. //
\\ While the variable 'b' has value of 1.5. \\
// Lastly, the variable 's' has value of "hi". //

Here is what I have so far, which clearly doesn't work...

a = int
b = folat
s = str
print(f"// The variable 'a' has value of <{a}> . //\n \\ While the variable 'b' has value of <{b}> . \\ \\n// Lastly, the variable 's' has value of <{s}>. //")

What changes should I make??

2 Answers2

1

You can just convert the int and float to a string and concatenate them all in the print statement. You also need to double the \ character, because it is the escape character, and acts as a single character with the proceeding character:

a = 1
b = 1.0
s = "a"
print("// The variable 'a' has value of " + str(a) + ". //\n\\\\ While the variable 'b' has value of " + str(b) + ". \\\\ \n// Lastly, the variable 's' has value of " + s + ". //")

Output:

// The variable 'a' has value of 1. //
\\ While the variable 'b' has value of 1.0. \\
// Lastly, the variable 's' has value of a. //
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23
1

Consider using the multiline priniting feature in python. Also you need to escape the \ because it itself is an escape character for example \n is newline instead of literally printing \n. And by doing \\ it means just \

a = 1
b = 1.5
s = "hi"

print(
f"""// The variable 'a' has value of {a}. //
\\\\ While the variable 'b' has value of {b}. \\\\
// Lastly, the variable 's' has value of "{s}". //"""
)

Personally i think this is the easiest to read and clean because it looks more like the desired output.

Muhd Mairaj
  • 557
  • 4
  • 13
  • 1
    The only thing missing here is to use `{s!r}` instead of `{s}`, to match up with expected output. But note that assumes OP is fine with the value being wrapped with a single quote - for dbl quotes, maybe better to manually wrap it instead, like `"{s}"`. – rv.kvetch Nov 28 '21 at 04:49
  • thank u for ur response, but would this work if a, b and c are not constant, like what if a,b and c are different, is there a code modification that I can do so that the code will work if a, b and c are changed WITHOUT modifying the a, b and c in the input? – jordan parker Nov 28 '21 at 04:50
  • @jordanparker you have a couple different options to handle it in that case. the simplest approach could be to wrap the `print` in a function, so `a,b,s` become local variables and inputs to the function instead; or you could use regular strings and use `str.format` to replace with values of var on each call. – rv.kvetch Nov 28 '21 at 04:54
  • @rv.kvtech Youre right. I missed that the string inputs have to be quoted – Muhd Mairaj Nov 28 '21 at 04:56
  • @jordan Pardon me, i didnt quite understand what youre asking for. Could you clarify it a bit? Perhaps add an edit to your question – Muhd Mairaj Nov 28 '21 at 04:58
  • ah its fine now thank you btw, I figured it out :) – jordan parker Nov 28 '21 at 07:10