Hello,

I'm very new to Python (meaning I just started class about a week ago) and I'm a little stuck on something that I'm hoping to get some direction on.

I'm trying to write a program that asks for user input for the names of two food names, then pushes them together to create a new word, outputting that to the user.

What I want to do is to make sure that the variables "food1" and "food2" cannot be the same exact thing. I thought to use an if/else statement, but I'm not sure how to express something along the same lines of "if food1 is the same thing as food2, output the statement 'You cannot use the same food twice.' and to ask for the user to input it again.

I think the problem is that the if/else statement seems to always be for mathematical expressions. Is there a way to perform this kind of statement with variable names alone as expressions?

Apologies for the noob question.

Also: Here's the code I'm using at the moment. Needless to say, the "=" is giving me a syntax error.

food1 = raw_input("Enter the name of one of your favorite foods: ")
print "One of your favorite foods is", food1 + "."

food2 = raw_input("Enter the name of another of your favorite foods: ")

if (food2 = food2):
    print "That was your first food! Please enter a DIFFERENT food."
    food2 = raw_input("Enter the name of another of your favorite foods: ")
else:
    print "Your other favorite food is", food2

fuppercase = food1.upper()

print "\n\nConsider this: If you were to have a food specially engineered just for you, it could be called", food1 + food "or" food2 + food1 "."
print "\nIf it were printed on a label designed to catch someone's attention, it would be called", fuppercase "."

raw_input("Press enter to exit the program.")

I know there are probably other errors in the code, but for now I'd like to just focus on the if/else statement.

Thanks!

Recommended Answers

All 5 Replies

Replace = (assignment operator) with == (comparison operator).

Member Avatar for sravan953

Also, if (food2 = food1):

Member Avatar for leegeorg07

and are you using python 30 or earlier? if so then you will need to use input("what is your favourite food?")

One thing that could really help you as well is to write it out in english form so that you can use that as a blueprint for your program. So for instance this project you are working on would look like.

1. Create 2 variables
2. Assign variable value through user input
ex. var = raw_input("question")
3. "Then pushes them together to create a new word"
Oh I need another variable for the new word. edit 1 and 2.
4. 'What I want to do is to make sure that the variables "food1" and "food2" cannot be the same exact thing.'
if they are not the same then run code
if var1 "is not eqal to" var2: #ex. if var1 != var2:
do these things
or else if they are the same error
else:
print an error message #ex. print(var1, "and", var2, "are the same error")
5. "You cannot use the same food twice.' and to ask for the user to input it again."
Keyword here is again, to run a peice of code over and over I need a loop.
Where do I want my loop? "cannot use same food twice"
That is where my if statement is at the moment.
Maybe a while loop?

I'll leave the rest to you, but this may help alot when you are first starting out, after a while you will go through that process in your head.

Thanks for all of the helpful advice and for pointing out the ginormous typographical error I had using food2 twice, everyone! x.x

I got it to work nicely, and I'll definitely be using the method you suggested, Willy.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.