英文:
How do I make this user input change the calculation in this if statement?
问题
Sure, here is the translated code with the requested changes:
print("我有以下行星信息:\n")
print(" 1. 金星 2. 火星 3. 木星")
print(" 4. 土星 5. 天王星 6. 海王星\n")
weight = 185
planet = 0
Planet_gravity = 0
金星 = 0.91
火星 = 0.38
木星 = 2.34
土星 = 1.06
天王星 = 0.92
海王星 = 1.19
Planet_name = input("行星名称: ")
print(Planet_name)
if Planet_name == "金星":
Planet_gravity += 0.91
elif Planet_name == "火星":
Planet_gravity += 0.38
elif Planet_name == "木星":
Planet_gravity += 2.34
elif Planet_name == "土星":
Planet_gravity += 1.06
elif Planet_name == "天王星":
Planet_gravity += 0.92
elif Planet_name == "海王星":
Planet_gravity += 1.19
planet_weight = weight * Planet_gravity
print("重力" + " " + (str(planet_weight)))
I've translated the code and made the necessary changes to compare the user input with the planet names in Chinese and calculate the correct planet weight based on the chosen input.
英文:
How do I make this user input change the calculation in this if statement?
print("I have information for the following planets:\n")
print(" 1. Venus 2. Mars 3. Jupiter")
print(" 4. Saturn 5. Uranus 6. Neptune\n")
weight = 185
planet = 0
Planet_gravity = 0
Venus = 0.91
Mars = 0.38
Jupiter = 2.34
Saturn = 1.06
Uranus = 0.92
Neptune = 1.19
Planet_name = input("Planet name: ")
print(Planet_name)
if Planet_name == Venus:
Planet_gravity += 0.91
elif planet == Mars:
Planet_gravity += 0.38
elif planet == Jupiter:
Planet_gravity += 2.34
elif planet == Saturn:
Planet_gravity += 1.06
elif planet == Uranus:
Planet_gravity += 0.92
elif planet == Neptune:
Planet_gravity += 1.19
planet_weight = weight * Planet_gravity
print("Gravity" + " " + (str(planet_weight)))
I would like the if statement to take into account the chosen user input and use it in the calculation to calculate planet weight. Everytime I type the name of the planet in the shell, I just get back 0 instead of the correct calculation. It's supposed to multiply the planet gravity by the weight to get the "planet_weight" as I call it or how heavy the person is on the planet.
答案1
得分: 0
Your if statement is incorrect. If you check a string, the literals need to be in quotes:
if planet_name == 'Venus':
Planet_gravity += 0.91
elif planet_name == 'Mars':
Planet_gravity += 0.38
and so on...
Another thing you can use is an f-string to print your output:
print(f"Gravity is: {planet_weight}")
Regards.
英文:
Your if statement is incorrect. If u check string, the literals need be in quotes
if planet_name == 'Venus':
Planet_gravity += 0.91
elif planet_name == 'Mars':
Planet_gravity += 0.38
and so on...
Another thing u can use f string for print ur out.
print(f"Gravity is : {planet_weight}")
Regards
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论