Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Need help with "unsupported operand type(s)"
#1
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
Yoriz wrote Oct-13-2019, 10:10 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
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]
Quote
#3
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
Quote
#4
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.
Quote
#5
input will return str.
You need to convert RemoveNameNumber to int in order to calculate index RemoveNameNumber - 1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote
#6
Finally got it - a big thanks for all the help, 1 down and 999999999999 to go Smile

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))

Quote
#7
I bit of advise - use string formatting instead of string concatenation. This will save you all the hassle to convert numbers to string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  calculate_bai -- TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' pantherd 1 505 Apr-21-2020, 12:31 PM
Last Post: anbu23
  Type hinting - return type based on parameter micseydel 2 411 Jan-14-2020, 01:20 AM
Last Post: micseydel
  TypeError: unsupported operand type(s) for -: 'str' and 'str' Balaji 4 3,360 Oct-11-2019, 10:29 AM
Last Post: buran
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 1,327 Sep-18-2019, 08:32 AM
Last Post: buran
  Type Error: bad operand type for unary +: 'str' Psypher1 2 5,590 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  please help me TypeError: unsupported operand type(s) for +=: 'int' and 'list' aankrose 2 2,750 Mar-26-2019, 06:00 PM
Last Post: perfringo
  unsupported operand type(s) Pythenx 5 3,498 Mar-21-2019, 09:42 PM
Last Post: Larz60+
  TypeError: unsupported operand type(s) for -: 'str' and 'str' shan1403 14 5,775 Dec-26-2018, 06:59 PM
Last Post: shan1403
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 1 14,223 Nov-20-2018, 03:02 AM
Last Post: stullis
  Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo kneesarethebees 1 1,580 Aug-02-2018, 01:39 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 2 Guest(s)