I am starting to learn python and I cannot figure out the following error "unsupported operand type(s) for -: 'str' and 'int'". Remember, I really don't know what I am to do here. Below is my code and error. Thanks ahead for any help in understanding what I don't know.
PUT CODE HERE
# list of names ---
ListOfNames = ["tom" , "bill" , "dick", "harry"]
print(ListOfNames)
# remove one name ---
RemoveNameNumber = input ("Enter the number of the name to remove: ")
print ("Verify number selected is " + str(RemoveNameNumber))
# name to be removed ---
NameRemoved = ListOfNames[RemoveNameNumber -1]
print("Name removed will be " + NameRemoved)
error:
Error:
11 # name to be removed ---
---> 12 NameRemoved = ListOfNames[RemoveNameNumber -1]
13 print("Name removed will be " + NameRemoved)
14
TypeError: unsupported operand type(s) for -: 'str' and 'int'
One more thing, if this is not the right way to post, please advise. Thanks
input returns a string, there is no need to use str on the string in the line
print ("Verify number selected is " + str(RemoveNameNumber))
instead use int on the returned string in the following line
NameRemoved = ListOfNames[RemoveNameNumber -1]
Hello
1- Line 7: The return type the input() function is a string so adding the str on the RemoveNameNumber variable is useless.
2- Line 10: The variable RemoveNameNumber has a string value because of the inpute() function, so you are trying to calculate a string to the number -1 (string - integer) which is an error;
- it has to be an integer to an integer, so you need to add the int to the RemoveNameNumber like this int(RemoveNameNumber).
hope this help
I still am not getting the str / int down pat. I still get the same error below and I have tried both str and int and it still fails - here my new code and error.
# LEARNING ABOUT "LIST"
# list of names ---
ListOfNames = ["tom" , "bill" , "dick", "harry"]
print(ListOfNames)
print("") #add a space for next line
# what is the total numbers in the list ---
TotalNumberInList = str(len(ListOfNames))
print ("Total number of names are: " + str(TotalNumberInList))
# keep number in range ---
print ("numbers to use must be between 0 and " + str(TotalNumberInList))
print("") #add a space for next line
# remove one name ---
RemoveNameNumber = input ("Enter the number of the name to remove: ")
print("") #add a space for next line
# name that will be removed ---
print ("Person being removed is " + ListOfNames[RemoveNameNumber -1])
# remove name from listing and print new listing ---
del ListOfNames[int(RemoveNameNumber [-1])]
print ("New List of Names are: " + str(ListOfNames))
and my error code -
20 # name that will be removed ---
21
---> 22 print ("Person being removed is " + ListOfNames[RemoveNameNumber -1])
23
24
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Again, thanks for all the help, I'll learn this yet. And this is a very good learning sample for me.
input will return str.
You need to convert RemoveNameNumber
to int in order to calculate index RemoveNameNumber - 1
Finally got it - a big
thanks for all the help, 1 down and 999999999999 to go
final code:
# LEARNING ABOUT "LIST"
# list of names ---
ListOfNames = ["tom" , "bill" , "dick", "harry"]
print(ListOfNames)
print("") #add a space for next line
# what is the total numbers in the list ---
TotalNumberInList = str(len(ListOfNames))
print ("Total number of names are: " + str(TotalNumberInList))
# keep number in range ---
print ("numbers to use must be between 1 and " + str(TotalNumberInList))
print("") #add a space for next line
# remove one name ---
RemoveNameNumber = int(input ("Enter the number of the name to remove: "))
print("") #add a space for next line
# name that will be removed ---
NameRemoved = ListOfNames[RemoveNameNumber -1]
print ("Person being removed is " + NameRemoved)
print("") #add a space for next line
# remove name from listing and print new listing ---
del ListOfNames[RemoveNameNumber -1]
print ("New List of Names are: " + str(ListOfNames))
I bit of advise - use
string formatting instead of string concatenation. This will save you all the hassle to convert numbers to string