Variable in range of float - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Variable in range of float (/thread-7193.html) |
Variable in range of float - dogginky - Dec-27-2017 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.') RE: Variable in range of float - Larz60+ - Dec-27-2017 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() RE: Variable in range of float - dogginky - Dec-27-2017 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() RE: Variable in range of float - Larz60+ - Dec-27-2017 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=search&term=mp3+player&submit=search and install the package you choose with: pip install packagenamecode would look somoething like: def play_music(self): your code here |