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 ()
Question:
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
Answers:
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.
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
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.