0

I'm sorry if this is confusing, I'm new to Python and this site and could use some help. I hope I'm clear with what I'm trying to get help with. When I try to get both of my user inputs printed, it gives me an error after I enter the food name. What am I doing wrong?

foodname=input('please enter food name.') # I ask the user for the name of their food
foodcost=input('please enter food cost.') # I ask for the cost of the same food
print foodname, foodcost #I print both of the given inputs

Here's the error message:

Traceback (most recent call last): 
File "C:/Python27/test.py", line 1, in <module> foodname=input('please enter food name.') 
File "<string>", line 1, in <module> NameError: name 'cheese' is not defined >>>
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Ali
  • 11
  • 1
    What is the error? – rawwar May 10 '18 at 00:32
  • Traceback (most recent call last): File "C:/Python27/test.py", line 1, in foodname=input('please enter food name.') File "", line 1, in NameError: name 'cheese' is not defined >>> – Ali May 10 '18 at 00:33
  • 3
    Your error is unrelated to the 3 lines of code you've posted above. We're going to need to see the rest of the file. The error does explain the issue though - you're using a variable/name `cheese` but it has not been defined – Ayush May 10 '18 at 00:33
  • That's the whole code. – Ali May 10 '18 at 00:34
  • 1
    maybe you are running the wrong file? – rawwar May 10 '18 at 00:35
  • I've ran it several times. ): – Ali May 10 '18 at 00:36
  • 3
    Your code is fine on Python 3, once you change that `print` staement into a `print()` function call. But it can give that error on Python 2. Solution: make sure you're running the Python 3 interpreter. – PM 2Ring May 10 '18 at 00:36
  • run it again and show us the screenshot of it. – rawwar May 10 '18 at 00:37
  • 1
    If you _have_ to use Python 2 then change those `input` calls to `raw_input`. – PM 2Ring May 10 '18 at 00:37
  • 1
    Thanks! Glad I got actual help, I tried it on 3.6 and it worked. Most people just ignore me and downvote my question, thinking that helps me any. Thanks again. – Ali May 10 '18 at 00:37
  • If you want help here, then include the relevant information in your post. If you need to add information, don't do so in comments; instead, [edit] your question and add it there where it can be seen. We're more than willing to help, but you need to provide the details. One of the primary details for Python is which version you're using, as there are significant differences between them. (I'd also suggest you avoid writing things like *Most people here*, even in comments, unless you're willing to get responses starting with *Most askers here*.) – Ken White May 10 '18 at 00:40
  • @KenWhite Good points, although in this case it's a fair guess that the OP is using Python 2, due to the `print` statement. Including the version tag would certainly be helpful; OTOH, not all new coders are aware of when that version info is necessary. – PM 2Ring May 10 '18 at 00:44
  • @PM2Ring: We shouldn't have to make a *fair guess*. The poster should include that relevant information. Now the poster is aware for when they have future questions here. :-) – Ken White May 10 '18 at 00:45
  • Downvoting my question doesn't make me want to ask more questions with more efforts, it bans me for several days from asking questions and I have to go elsewhere for help. It does more harm than good. – Ali May 10 '18 at 00:55

1 Answers1

0

The input and print syntax is slightly different between python 2.7 and 3. In 2.7 input evaluates the data, whereas raw_input reads it in as a string. In 3.x input will read in the data an as a string. print doesn't need brackets in 2.7 but needs them in 3.x

Python 2.7

foodname=raw_input('please enter food name.') # raw_input returns a string
foodcost=int(raw_input('please enter food cost.')) # read as string cast to int
print foodname, foodcost 

Python 3.x

foodname=input('please enter food name.') # Input returns a string
foodcost=int(input('please enter food cost.')) 
print(foodname, foodcost) # add brackets to print
ktzr
  • 1,625
  • 1
  • 11
  • 25
  • Sure, but please don't encourage people to use Python 2 `input`. It's much better to use `raw_input` and do an explicit `float` or `int` conversion on the user input string. – PM 2Ring May 10 '18 at 00:46