Mark E.D. Upton Mark E.D. Upton - 1 year ago 126
Python Question

TypeError: tuple indices must be integers, not str, French Translation program

I'm writing a french translation program that is converting the english words into the french words and the user is in charge of this process. The problematic code is outlined with an arrow and a hashtag, approxiamately line 25 in my program.

count = 1
leave = True
englist = ("Pepper", "Asparagas", "Parsnip",
"Map", "Knee", "Ice", "Handsome", "Fox", "Price", "Never", "Liar",
"Joke", "House", "Garlic", "Cow", "Pig", "Mouse", "Lamb", "January",

frenchlist = ("Poivre", "Asperge", "Panais", "Carte", "Genou",
"Glace", "Beau", "Renard", "Prix", "Jamais", "Menteur", "Plaisanterie",
"Maison", "Ail", "Vache", "Cochon", "Souris", "Agneau", "Janvier",
for i in range(len(englist)):
print str(i+1)+".",englist[i]
while leave:
anumaux = raw_input ("Pick a number from one to twenty to indicate an English word you want to translate.")
valid = False
while not valid:
if anumaux.isdigit():
if anumaux >= MINNUM and anumaux < MAXNUM:
if englist[(anumaux)-1]!="***":
valid = True
wordbfrtrans = englist[anumaux-1] # <---- Problem code
frtrans = raw_input ("What is the the French translation of "+str(wordbfrtrans)+"? ")

This is the result of this code when ran:

1. Pepper
2. Asparagas
3. Parsnip
4. Map
5. Knee
6. Ice
7. Handsome
8. Fox
9. Price
10. Never
11. Liar
12. Joke
13. House
14. Garlic
15. Cow
16. Pig
17. Mouse
18. Lamb
19. January
20. Hen
Pick a number from one to twenty to indicate an English word you want to translate.3

Traceback (most recent call last):
File "/Users/Surprise/Downloads/List Assignment 2 (1).py", line 25, in <module>
wordbfrtrans = englist[anumaux-1] # <---- Problem code
TypeError: unsupported operand type(s) for -: 'str' and 'int

Thanks to anybody helping out this late!

Answer Source

The variable anumaux must be converted to an integer. Try wordbftrans = englist[int(anumaux)-1] instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download