Python Forum
if/else statement only outputs else statement regardless of input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if/else statement only outputs else statement regardless of input
#1
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
Reply
#2
row # 6 - input returns string
row # 21 - you compare string to int. They cant be equal.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I did end up fixing this simply by putting the 1 in quotes (if CS == "1":) and that worked. Thanks all!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statement question help Gatso100 4 606 Jan-09-2024, 11:53 AM
Last Post: Pedroski55
  Can't stop if statement from executing even though False + Messy code? monkeydesu 1 949 Oct-17-2022, 05:46 AM
Last Post: deanhystad
  Post IF Statement Input OrphanedSoul 2 2,275 Jun-04-2021, 05:03 PM
Last Post: deanhystad
  if statement not working g0g0g1g 2 1,607 Sep-08-2020, 05:40 PM
Last Post: nilamo
  While statement explanation alkhufu2 3 2,339 Sep-02-2020, 05:46 PM
Last Post: alkhufu2
  I need help to solve this task using while statement rico4pepe 6 9,193 Apr-02-2020, 11:34 AM
Last Post: pyzyx3qwerty
  Filtering with IF Statement Mike2607 10 5,115 Nov-29-2019, 07:18 PM
Last Post: perfringo
  Trying to get an if..elif..else statement to run. Azurato 4 2,566 Jul-29-2019, 12:17 PM
Last Post: ichabod801
  Else Statement Not Working SenkouSimmer 4 3,175 Jul-22-2019, 11:42 AM
Last Post: jefsummers
  Help for newbie with if/else statement that has or statement Shevach 2 2,071 May-08-2019, 09:00 PM
Last Post: Shevach

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020