Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Need to use range with decimals KameronG Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-08-2019, 03:15 PM Hello, I need to write a program that asks for values from a user and then use the values in a formula. One thing I need to do is ask the user if they want a range, list, or single value for one of the parameters, but I can't figure out how to do this. I don't think I'm allowed to use numpy for this. Here is my code: ```import math h = float(input("What is the value of H0 (in km/s/Mpc)?")) z = float(input("What is the value of the redshift?") 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)?") ran = input("Do you want a range of readshifts (min, max, increment), or a list (type range, list, o\$ if ran == "range": i = float(input("What is the minimum of the range you desire (enter min z value)?")) k = float(input("What is the maximum of the range you desire (enter max z value)?")) u = float(input("By what increment do you want z to be calculated (enter increment size)?")) for z in range(i, k, u): z = z + u ```There's more after that, but I don't want to post my entire code for this issue unless I have to. So, the range gives me SOMETHING if I give it only integers, but it only gives me one value and I don't know why. For example, if I use (1, 3, 1), it only gives one value instead of 3. Beyond this, using integers only makes the range pointless as redshifts aren't often integers.If anyone knows a way this can be done, I greatly appreciate it. Thank you. Larz60+ aetate et sapientia Posts: 7,075 Threads: 276 Joined: Sep 2016 Reputation: 250 Likes received: 1003 #2 Feb-08-2019, 04:01 PM Quote:for one of the parameters, which one? ichabod801 Bunny Rabbit Posts: 3,021 Threads: 61 Joined: Sep 2016 Reputation: 155 Likes received: 927 #3 Feb-08-2019, 04:07 PM If you want to build the list based on the user providing a min, max, and increment, you don't need to use range: ```data = [] current = min_value while current < max_value: data.append(current) current += increment ``` Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures woooee Minister of Silly Walks Posts: 387 Threads: 0 Joined: Feb 2018 Reputation: 25 Likes received: 35 #4 Feb-08-2019, 07:14 PM A range is a list, and a list can have a single value. KameronG Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #5 Feb-08-2019, 07:31 PM Ok,so now I have this: ```if ran == "range": i = float(input("What is the minimum z value?")) k = float(input("What is the maximum z value?")) u = float(input("By what increment?")) data = [] current = i while i < k: data.append(i) current += u ```But after asking for the min, max and increment, nothing happens. I'm guessing I need something in the brackets after data, but I'm not sure what would go there. ichabod801 Bunny Rabbit Posts: 3,021 Threads: 61 Joined: Sep 2016 Reputation: 155 Likes received: 927 #6 Feb-08-2019, 07:47 PM Well, you haven't shown us the code after that section, so we have no way of knowing what should happen. What inputs are you using, and what is the value of data after the loop? Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures KameronG Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #7 Feb-08-2019, 07:49 PM Here is the entire program: ```import math h = float(input("What is the value of H0 (in km/s/Mpc)?")) 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)?") ran = input("Do you want a range of readshifts (min, max, increment), or a list (type range, list, o\$ if ran == "range": i = float(input("What is the minimum z value?")) k = float(input("What is the maximum z value?")) u = float(input("By what increment?")) z = i data = [z] while i < k: data.append(i) z += u 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) ``` ichabod801 Bunny Rabbit Posts: 3,021 Threads: 61 Joined: Sep 2016 Reputation: 155 Likes received: 927 #8 Feb-08-2019, 07:57 PM You create the list data, and then you don't do anything with it. Nothing is going to happen with data if you don't use it. And those single letter variable names make your code really hard to read. Please try to use more descriptive variable names. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Use range to add char. to empty string johneven 4 117 Apr-15-2019, 11:21 PM Last Post: scidam switch case with range jun 3 163 Feb-27-2019, 05:35 AM Last Post: scidam IndexError: list index out of range abdullahali 4 294 Jan-17-2019, 07:54 AM Last Post: buran python homework print the sum of a range of numbers from x to y kirito85 3 329 Oct-28-2018, 08:56 AM Last Post: kirito85 Python Tutorial range function question HawkeyeKnight 2 355 Sep-27-2018, 09:45 AM Last Post: ThiefOfTime String index out of range felie04 2 713 Aug-17-2018, 11:18 PM Last Post: felie04 Accessing data in zip - Index out of range pythoneer 24 2,575 Mar-15-2018, 06:19 PM Last Post: buran Debugging error, while loop out of range zenort 1 1,041 Feb-14-2018, 03:36 PM Last Post: zenort Questions on lists, the range function & concatenation fad3r 7 885 Jan-25-2018, 03:11 PM Last Post: fad3r Iterating over pandas.df to check for values out of range Padowan 14 3,763 Nov-26-2017, 04:37 PM Last Post: heiner55

Forum Jump:

Users browsing this thread: 1 Guest(s)