-2

I keep getting an error on the line with the #. I've tried putting "" around each symbol, all the symbols together, and put the symbols in ().

def main():

  name = input("Enter a reader's name: ")
  symbol = input("Enter a symbol: ")
  rating = input("Enter a rating: ")

  if name not in 'ratings.txt':
    print("No such reader " + name)
    print("bye!")
    return
  #if symbol not >,<,=:
    print("please use >,<,=")
    return
  if rating not -5,-3,0,1,3:
    print("please use -5,-3,0,1,3")
    return
  if name = []:
    print('no books found')
    return
 
tdelaney
  • 73,364
  • 6
  • 83
  • 116
cdj
  • 3
  • 1
  • 1
    Show the full traceback of the error as properly formatted text in the question. Show the code as it really is without a marker `#` which changes the code substantially (you can put it at the end of the line). – Michael Butscher Aug 27 '22 at 00:02
  • 1
    Welcome to Stack Overflow. Please read [ask] and make sure to **ask a question** when posting - please don't make us guess. If you are "getting an error", [show the error](https://meta.stackoverflow.com/questions/359146). If the code doesn't do what you want it to do, tell us what you want it to do. Then ask, starting with a question word like "why" or "how" and ending with a question mark (`?`). – Karl Knechtel Aug 27 '22 at 00:12
  • 1
    There are many problems with this code and it is very unlikely that it does anything even close to what you want. The *error* occurs because, in order to write a **string**, you need quotes around it - `<` means an actual comparison operation, `'<'` means a string with that symbol in it. – Karl Knechtel Aug 27 '22 at 00:15

1 Answers1

0

You can use the not in construct to check if an item is not in a list/tuple/other container. Thus, you can do this:

item = "foo"
print(item not in ["foo", "bar", "baz"]) # False
print(item not in ["ham", "egg", "pan"]) # True

This works for numbers too. However, using sets (with curly braces) is more efficient if all you're doing is testing if an item is in the container. Also, I see your code has

if name = []:

When testing for equality in an if statment, use the equals operator ==. Thus your code will be:

  name = input("Enter a reader's name: ")
  symbol = input("Enter a symbol: ")
  rating = input("Enter a rating: ")

  if name not in 'ratings.txt':
    print("No such reader " + name)
    print("bye!")
    return
  if symbol not in {">", "<", "="}:
    print("please use >,<,=")
    return
  if rating not in {-5, -3, 0, 1, 3}:
    print("please use -5,-3,0,1,3")
    return
  if name == []:
    print('no books found')
    return

Finally, the line

if name not in "ratings.txt":

does not check if name is in the contents of the file ratings.txt, but rather if it is a substring of that filename. For checking the file contents, you can try this question.

pigrammer
  • 2,603
  • 1
  • 11
  • 24
  • Welcome to Stack Overflow. The effort is appreciated, but please read [answer]. Someone with this many issues in the code does not have an answerable question (BTW, good job spotting `name = []`, but as `name` is a string, it can never be equal to `[]` anyway), and the main problem highlighted (checking whether `symbol` is one of multiple candidates) is a common duplicate. Now that you have 50 reputation, you can comment on questions to ask for clarity and point out other issues with questions. – Karl Knechtel Aug 27 '22 at 00:17