Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text variables won't add
#1
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.
Reply


Messages In This Thread
Text variables won't add - by st3m0n - May-30-2018, 02:58 PM
RE: Text variables won't add - by wavic - May-30-2018, 03:26 PM
RE: Text variables won't add - by Larz60+ - May-30-2018, 03:26 PM
RE: Text variables won't add - by ljmetzger - May-30-2018, 03:29 PM
RE: Text variables won't add - by j.crater - May-30-2018, 04:09 PM
RE: Text variables won't add - by st3m0n - May-30-2018, 06:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  variables from a text file vlk 2 2,318 Sep-22-2019, 06:44 PM
Last Post: vlk
  Format text and variables - more lines dan789 3 2,528 Jan-08-2019, 03:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020