Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 if/else statement only outputs else statement regardless of input KameronG Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-08-2019, 06:18 AM 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 perfringo Giant Foot Posts: 961 Threads: 2 Joined: Jun 2018 Reputation: 98 Likes received: 218 #2 Feb-08-2019, 08:01 AM 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 Life of Brian: Conjugate the verb, "to go" ! KameronG Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #3 Feb-08-2019, 08:04 AM I did end up fixing this simply by putting the 1 in quotes (if CS == "1":) and that worked. Thanks all! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Trying to get an if..elif..else statement to run. Azurato 4 239 Jul-29-2019, 12:17 PM Last Post: ichabod801 Else Statement Not Working SenkouSimmer 4 300 Jul-22-2019, 11:42 AM Last Post: jefsummers Help for newbie with if/else statement that has or statement Shevach 2 250 May-08-2019, 09:00 PM Last Post: Shevach syntax error in an if statement at the else ? Just_started 2 269 Mar-07-2019, 08:35 PM Last Post: Just_started Decision statement problems erfanakbari1 2 315 Mar-06-2019, 12:27 AM Last Post: woooee How to print a statement if a user's calculated number is between two floats Bruizeh 2 316 Feb-10-2019, 12:21 PM Last Post: DeaD_EyE While loop and If Statement farzankh 3 435 Jan-27-2019, 10:13 PM Last Post: stullis A three if statement problem I am having WarblingPasta 1 428 Nov-01-2018, 03:04 PM Last Post: j.crater How to force print statement to print on one line wlsa 4 559 Oct-28-2018, 09:39 PM Last Post: wavic Need of return in function if statement inside the function already returns Athul 5 820 Aug-16-2018, 10:19 AM Last Post: DuaneJack

Forum Jump:

Users browsing this thread: 1 Guest(s)