Hack Forums
[Python] Having trouble outputting simple text. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Having trouble outputting simple text. (/showthread.php?tid=5785676)



Having trouble outputting simple text. - Mik3y - Jan 16, 2018

This is from our very first class, so I apologize for the simple cringe.  

I am trying to write a program in Python that will output my name, favorite color, and number.  (Mike, blue, and 13)


Code
>>> my_name = "Mike"
>>> fav_color = "blue"
>>> lucky_num = "13"
print(my_name, "favorite color is", fav_color, "and lucky number is", lucky_num)


I was hoping that this would give me the output of: "Mike favorite color is blue and lucky number is 13"

------------------
To cheat I did this:

Code
print("Mike favorite color is blue and lucky number is 13")

Any help would be appreciated guys.  

**the class meets once a week and this is our only assignment this week.


RE: Having trouble outputting simple text. - XxClassifiedxX - Jan 16, 2018

you need to use a "+" instead of commas when using the variables


RE: Having trouble outputting simple text. - Mik3y - Jan 16, 2018

(Jan 16, 2018 12:03 PM)XxClassifiedxX Wrote:  you need to use a "+" instead of commas when using the variables

So something like this?


Code
my_name = "Mike"
fav_color = "blue"
lucky_num = "13"
print(my_name+"favorite color is", fav_color+"and lucky number is"+lucky_num)



RE: Having trouble outputting simple text. - TheMarkMan - Jan 16, 2018

I like to use the .format function that belongs to strings. So you could do this/
Code
print(“{} favorite color is {} and his lucky number is {}”.format(“Mike”,”blue”,13))



RE: Having trouble outputting simple text. - Anxuein - Jan 16, 2018

(Jan 16, 2018 02:00 PM)TheMarkMan Wrote:  I like to use the .format function that belongs to strings. So you could do this/
Code
print(“{} favorite color is {} and his lucky number is {}”.format(“Mike”,”blue”,13))

Please use this rather than concatenating strings


RE: Having trouble outputting simple text. - Vitozdar - Jan 20, 2018

use + instead of commas, it will work then


RE: Having trouble outputting simple text. - ⁠Cammy - Jan 21, 2018

(Jan 16, 2018 01:02 PM)Mik3y Wrote:  So something like this?


Code
my_name = "Mike"
fav_color = "blue"
lucky_num = "13"
print(my_name+"favorite color is", fav_color+"and lucky number is"+lucky_num)

PHP Code

<?php 
my_name 
= "Mike"
fav_color = "blue"
lucky_num = "13"
print(my_name + " favorite color is " + fav_color + " and lucky number is " + lucky_num) 

But please use string.format instead as suggested above


RE: Having trouble outputting simple text. - Dialog - Jan 21, 2018

(Jan 21, 2018 07:51 AM)⁠Cammy Wrote:  
PHP Code

<?php 
my_name 
= "Mike"
fav_color = "blue"
lucky_num = "13"
print(my_name + " favorite color is " + fav_color + " and lucky number is " + lucky_num) 

But please use string.format instead as suggested above

So you're giving an example that he should, in your opinion, not use, and instead recommend something that you acknowledge has already been posted? Why are you even posting? No surprise you're at 13k post count.


RE: Having trouble outputting simple text. - ⁠Cammy - Jan 21, 2018

(Jan 21, 2018 09:57 AM)Dialog Wrote:  So you're giving an example that he should, in your opinion, not use, and instead recommend something that you acknowledge has already been posted? Why are you even posting? No surprise you're at 13k post count.

Because the example I posted is what OP was trying to accomplish with the commas and nobody gave a proper example. It's always good to know about multiple approaches, which ones you shouldn't use and which ones you should. But thanks for your input, prick.


RE: Having trouble outputting simple text. - Annointed - Jan 22, 2018

(Jan 16, 2018 02:00 PM)TheMarkMan Wrote:  I like to use the .format function that belongs to strings. So you could do this/
Code
print(“{} favorite color is {} and his lucky number is {}”.format(“Mike”,”blue”,13))

This would be correct, but you should use your defined variables instead, incase the string value's change.

Code
print(“{}, favorite color is {}, and his lucky number is {}”.format(my_name, fav_color, lucky_num))