Problem with if statement branching after Python input

Asked 2 years ago, Updated 2 years ago, 47 views

a=input("What do you want to calculate? 1. Molar concentration 2. PPM concentration 3. Percent concentration")

if a==1:
                b=input ("What is the concentration of the solution you want to calculate?")
                c=input ("How many moles of solution do you want to calculate?"")
                d=c/b
                print("The molar concentration is ""d,"" and //kg.")

elif a==2:
                e=input ("What is the mass of the solute you want to calculate?"")
                f=input ("What is the mass of the solution you want to calculate?"")
                g=e/f*10^6
                print("PPM concentration is ""g,"PPM.")

elif a==3:
                h=input ("What is the mass of the solute you want to calculate?"")
                i=input ("What is the mass of the solution you want to calculate?"")
                k=h/i*100
                print("% concentration is ""k",%""

There is no error, but if you actually execute it, the first INPUT door pops up, but if you enter 1, it doesn't pop up after that. Please let me know where the error is.

python input

2022-09-20 15:39

2 Answers

` #1 a=int("What do you want to calculate?" 1. Molar concentration 2. PPM concentration 3. Percent concentration")

#2 a = map(int,input("What do you want to calculate? 1. molar concentration 2. PPM concentration 3.percent concentration") `

Both forms are possible.

As explained above, the return of input() is always a string. Therefore, expressions belonging to the if, elif, and else statements must also be modified to int(input()) or float(input()).


2022-09-20 15:39

It is recommended that you always memorize the return of Python input() as a string.

if int(a) == 1: Please convert all integers.


2022-09-20 15:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.