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
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
change line 11 to read:
print("\nnewfood: {}".format(newfood))
Reply
#4
The problem is your print statements. Try something like:
print("newfood is: {}".format(newfood))
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#5
Just a question, following great answers provided by other posters...
How do you even get
"cheese"
and
"cake"

printed with

print("food1")
  
print("food2")
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  variables from a text file vlk 2 2,187 Sep-22-2019, 06:44 PM
Last Post: vlk
  Format text and variables - more lines dan789 3 2,424 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