Python Forum
Text variables won't add - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Text variables won't add (/thread-10659.html)



Text variables won't add - st3m0n - May-30-2018

In the book I'm learning python programming from, there is an exercise where I have to gain user input on their two favourite foods to create a name for a new food. Most of the code works, but I can't get the two food names to add together despite following the similar codes previously demonstrated in the book (which all worked fine) as best I can. Even if I put in numbers instead of words it doesn't work. I fear I am somehow missing something very simple, though it could be to do with the fact that book is written for Python 3.1, and I am using Python 3.6.5. This is the code I put in:

food1 = input("What is your favourite food? ")

print("food1")

food2 = input("What is your second favourite food? ")

print("food2")

newfood = food1 + food2

print("\nnewfood:")

input("\n\nPress the enter key to exit")
If I were to put cheese as the favourite food, and cake as the second favourite it should create a "newfood" and come out with either cheesecake, or cheese cake (but I think it would be the first). What I actually get is:

What is your favourite food? cheese
cheese
What is your second favourite food? cake
cake

new food:


Press the enter key to exit

Any help you can give me would be greatly appreciated, also, if you can tell me whether I got it wrong due to an oversight, or just because of the difference between python 3.1 and 3.6.5, that should be helpful in the way I approach other problems in the future. Thanks in advance for looking at my thread, and for any help you can give me.

Stephen.


RE: Text variables won't add - wavic - May-30-2018

You do not print newfood object. Instead, a string is printed. Perhaps you need:
print('\n', newfood) # bad "formatting" but for the example is good enough



RE: Text variables won't add - Larz60+ - May-30-2018

change line 11 to read:
print("\nnewfood: {}".format(newfood))



RE: Text variables won't add - ljmetzger - May-30-2018

The problem is your print statements. Try something like:
print("newfood is: {}".format(newfood))
Lewis


RE: Text variables won't add - j.crater - May-30-2018

Just a question, following great answers provided by other posters...
How do you even get
"cheese"
and
"cake"

printed with

print("food1")
  
print("food2")



RE: Text variables won't add - st3m0n - May-30-2018

Thanks guys, managed to get it working :).
@j.crater, I unintentionally put quote marks in when I wrote it out on here, they're not in the programming I did, my bad.