Python Forum
if/else statement only outputs else statement regardless of input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: if/else statement only outputs else statement regardless of input (/thread-15968.html)



if/else statement only outputs else statement regardless of input - KameronG - Feb-08-2019

Hello, I'm new here so I apologize if I mess up the protocol to some extent.
I am trying to write a program that takes 4 user inputs and plugs them into a formula. The last prompt to insert input asks if the user wants "flat cosmology" or "open cosmology" (represented by entering a 1 or 0 respectively). For some reason, regardless if I enter 1 or zero, it gives the value for if 0 is chosen. I will paste the code here:

import math

h = float(input("What is the value of H0 (in km/s/Mpc)?"))
z = float(input("What is the value of z?"))
j = float(input("what is the value of the matter density parameter?"))
cs = input("Do you want a flat or open cosmology (1 = flat, 0 = open)?")

def eta(a, omega):
    s = ((1- omega)/omega)**(1/3)
    return 2*math.sqrt(s**3 + 1)*(1/(a**4) - 0.01540*s/(a**3) + 0.4304*(s**2)/(a**2)\
                                  + 0.19097*(s**3)/a +0.066942*s**4)**(-1/8)
c = 3e8
q = (j/2)

d1 = (1/(1 + z))*(c/h)*(eta(1, j) - eta(1/(1 +z), j))
d1 = d1*(math.pi/180)*(1/3600)

d2 = (c/(h*q**2))*(((z*q) + (q - 1)*(math.sqrt(2*z*q + 1) - 1))/((1 + z)**2))
d2 = d2*(math.pi/180)*(1/3600)

if cs == 1:
    print(d1)
else:
    print(d2)      
so, it will only print (d2) no matter what I enter into the last prompt, and I'm not sure why. I'm running this on the debian distribution of Linux.

If I remove the (d2), if, and else statements altogether, it gives the correct value for
cs == 1.

If anyone can help me figure out what's going on here, I would greatly appreciate it.
Thank you very much!



Kameron


RE: if/else statement only outputs else statement regardless of input - perfringo - Feb-08-2019

row # 6 - input returns string
row # 21 - you compare string to int. They cant be equal.


RE: if/else statement only outputs else statement regardless of input - KameronG - Feb-08-2019

I did end up fixing this simply by putting the 1 in quotes (if CS == "1":) and that worked. Thanks all!