我正在编写法语翻译程序,将英语单词转换为法语单词,用户负责此过程。有问题的代码用箭头和主题标签概述,大约在我的程序中的第25行。
MINNUM = 1
MAXNUM = 21
count = 1
leave = True
englist = ("Pepper", "Asparagas", "Parsnip",
"Map", "Knee", "Ice", "Handsome", "Fox", "Price", "Never", "Liar",
"Joke", "House", "Garlic", "Cow", "Pig", "Mouse", "Lamb", "January",
"Hen")
frenchlist = ("Poivre", "Asperge", "Panais", "Carte", "Genou",
"Glace", "Beau", "Renard", "Prix", "Jamais", "Menteur", "Plaisanterie",
"Maison", "Ail", "Vache", "Cochon", "Souris", "Agneau", "Janvier",
"Poule")
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)+"? ")
这是运行时此代码的结果:
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
答案 0 :(得分:0)
变量anumaux
必须转换为整数。请改为wordbftrans = englist[int(anumaux)-1]
。
答案 1 :(得分:0)
raw_input
返回一个字符串use int(anumaux)
,将字符串转换为整数