此代码的目标是要求编号1-41,在文本文档中查找该特定数字行,并检查它是否等于字符串&#39; good&#39;。< / p>
f = open("Goods", "r") #Opens the "Goods" file
lines = f.readlines() #Loads all 42 lines into memory
x = int(input("pick a number 1-41")) #asks for a number 1-41, casts it to an int, and assigns it to 'x'
if x < 13:
goods = "Lumber"
elif x > 12 and x < 22:
goods = "Spices"
elif x > 21 and x < 33:
goods = "Fruit"
elif x > 32 and x < 41:
goods = "Rum"
else:
goods = "Opium"
if lines[x] == string:
print("It worked!")
else:
print(string, lines[x])
#prints both the string, and the line gotten from the text document for trouble shooting
print("Fail")
然而,这段代码的问题在于,即使文本文档中的行和字符串“&#39; goods&#39;完全一样; python没有看到,而是执行else语句。
如何让python将两个字符串注册为相同的?