Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+7 votes

Here is what I have. I am not sure what is wrong. After playing with several combinations of spaces and quotation marks, I still don't know what is wrong. My error messages tell me that the problem lies within the last line, that the syntax is wrong. Please help if you know :( #TeamGandalf

def tree():
    tree = eval(input("Enter your favorite tree: "))

def food():
    food = eval(input("Enter your favorite food: "))

print("Do you want to eat magical", food, "in an enchanted", tree, "with me?")
asked in CSC201 Spring 2021 by (8 points)
edited by

I dont think you need to have the eval() in front of the input.
Try it without it

3 Answers

+5 votes

If you wanna enter a string of text, you will use this input function:
variable = input(‘words describing what to enter‘)
Ex: tree = input('Enter your favorite tree: ')

If you wanna enter an integer number, you will use this input function:
variable = int(input(‘words describing what to enter‘))
variable = eval(input(‘words describing what to enter‘))
Ex: numberoftrees = int(input('What is the total number of trees in the world: '))

Be careful between those above input functions and determine what type of input value (text or number) you wanna enter.

Hope this helps you avoid this error next time!

answered by (8 points)
+5 votes

eval() tries to convert whatever input is given into an integer(number).
so if the user enters '5', it is converted from just text to an actual value for the code to use. you only need it if the variable you define(food/tree in this case) is a number.

also, it looks like you are defining tree() and food() as functions, which is unnecessary—you generally want to define a single function (commonly called main()) which contains the lines for defining tree and food, and then prints the text you want.

answered by (8 points)
+4 votes

Do you need the eval before input? Maybe try without and see if it helps.

answered by (125 points)