如何使用户输入改变此 if 语句中的计算?

huangapple go评论68阅读模式
英文:

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}")

print string with numbers

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}")

print string with numbers

Regards

huangapple
  • 本文由 发表于 2023年6月9日 09:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定