Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable in range of float
#1
Noob here just a few weeks in, So after all the reading and videos I decided it was time to do more writing.
I got side tracked and somehow ended up trying to take a variable and turn it into a float that I could check to see if it was between two floats values.
I seen where someone used pylab frange for something and got it to work right I think. Keep in mind I m very new. How do I clean this up or make a function,
Where do I go from here or put it to use....

import pylab as pl # range scale to determine outcome of a weighted purchase avg_screwed = pl.frange(0.00,6.0,0.1) avg_below = pl.frange(6.0,7.0,0.1) avg_weight = pl.frange(7.0,7.50,0.1) avg_above = pl.frange(7.50,8.0,0.1) avg_extreme = pl.frange(8.0,99.0,0.1) #get users purchase weight input user_weight = float(input('What was the weight: ')) #possible outcomes to inform the user of there purchase worth if user_weight in avg_weight:    print("You money was well spent.") elif user_weight in avg_above:    print('Nice, You did really well!') elif user_weight in avg_extreme:    print("WOW! Someone liked you.") elif user_weight in avg_below:    print('You have been shorted.') elif user_weight in avg_screwed:    print('Turn around and bend over!!!!!!') else:    print('I do not understand.')
Reply
#2
import pylab as pl class WeightedPurchase: def __init__(self): # range scale to determine outcome of a weighted purchase self.avg_screwed = pl.frange(0.00, 6.0, 0.1) self.avg_below = pl.frange(6.0, 7.0, 0.1) self.avg_weight = pl.frange(7.0, 7.50, 0.1) self.avg_above = pl.frange(7.50, 8.0, 0.1) self.avg_extreme = pl.frange(8.0, 99.0, 0.1) def get_user_pweight(self): user_weight = None while True: try: # get users purchase weight input user_weight = float(input('What was the weight (zero to exit): ')) return user_weight except: print('please enter numeric weight') def make_comment(self): while True: user_weight = self.get_user_pweight() # possible outcomes to inform the user of there purchase worth print(f'user_weight: {user_weight}') if user_weight == 0: break elif user_weight in self.avg_weight: print("You money was well spent.") elif user_weight in self.avg_above: print('Nice, You did really well!') elif user_weight in self.avg_extreme: print("WOW! Someone liked you.") elif user_weight in self.avg_below: print('You have been shorted.') elif user_weight in self.6.5 avg_screwed: print('Turn around and bend over!!!!!!') else: print('I do not understand.') def main(): wp = WeightedPurchase() wp.make_comment() if __name__ == '__main__': main()
Reply
#3
thank you, I see I have a lot of code to study now, I remember some of what you wrote from other lessons.
Could I have had the if run a  function(assuming I had made one called play_music,,, instead of printing to the screen.
Or if I wanted to have a different calculation for each input depending on the range it was in. I would just make functions for each if ?
example below...Sry if does not make since. Ill give my self 24 to digest what you wrote
           elif user_weight in self.avg_weight:                play_music()
Reply
#4
Sure, but play_music would have to have name of mp3 or whatever format you were using,
plus you would need to load a music package which is easy.
Find what interest you here: https://pypi.python.org/pypi?%3Aaction=s...mit=search
and install the package you choose with:
pip install packagename
code would look somoething like:

    def play_music(self):        your code here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 3,148 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  matplotlib x axis range goes over the set range Pedroski55 5 8,480 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 12,151 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 5,634 Jul-25-2019, 11:32 PM
Last Post: scidam
  Inclusive (closed) range with float numbers mapg 4 7,167 Apr-27-2019, 09:09 PM
Last Post: Gribouillis
  Variable Range Trinx 4 4,242 Jan-10-2019, 03:48 AM
Last Post: Trinx
  Would like to input a date variable and determine whether it is within the time range harold 3 3,899 Jan-05-2019, 09:04 AM
Last Post: Gribouillis
  Modifying / extracting multiple list items simultaneously using variable from range ehammarlund 4 5,368 Dec-06-2017, 08:15 PM
Last Post: ehammarlund

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.