Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Having trouble outputting simple text.

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.
you need to use a "+" instead of commas when using the variables
(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)
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))
(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

hi
use + instead of commas, it will work then
(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
(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.
(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.
(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))


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by pebblakOct 15, 2019 05:24 AM 6 305 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux
[C] by Aus10Feb 7, 2016 06:25 PM 4 149 Feb 7, 2016 06:25 PM
Last Post: Aus10
[.NET] by SmokeyModemJan 20, 2016 06:21 PM 2 70 Jan 20, 2016 06:21 PM
Last Post: Hawk HF