所以我想做一個程序來計算不同星球上的體重。這很令人沮喪,因爲它不能正確計算。If else statement planets
if ("Mercury" or "mercury" == planetName):
weight = weightObject * mercury
elif ("Venus" or "VEnus" == planetName):
weight = weightObject * venus
elif ("Earth's Moon" or "Earths Moon" == planetName):
weight = weightObject * earthsmoon
elif ("Mars" or "MArs" or "MARS" == planetName):
weight = weightObject * mars
elif ("Jupiter" or "JUpiter" == planetName):
weight = weightObject * jupiter
elif ("Saturn" or "saturn" == planetName):
weight = weightObject * saturn
elif ("uranus" or "Uranus" == planetName):
weight = weightObject * uranus
elif ("neptune" or "Neptune" == planetName):
weight = weightObject * neptune
elif ("pluto" or "Pluto" == planetName):
weight = weightObject * pluto
else:
weight = -1
#To print out the planet and weight and make sure its a planet and non negative number
#It will not calculate a negative weight or different planet than listed
if (weightObject > 0):
print("The weight of the object on",planetName,"is {0:,.2f}".format(weight))
else:
print("Error: Planet name not found or number was negative. Please try again.")
如果我爲每個星球輸入20.5,它會給我所有人的完全相同的數字。有人可以幫忙嗎?
你可以發佈完整的代碼來測試它,或者它是一個功能完整的部分,如果它太長。 – Leb
你可以發佈你的所有代碼嗎? – PolarisUser
爲什麼不使用字典來將星球名稱映射到權重因子,而不是所有那些如果語句? – Barmar