Reputation: 321
I'm making a currency converter. The converter needs a min-range and max-range where the converter will ask you to change the ranges to able to convert the currency if the value of currecy is above or under those ranges. For example if the user sets the ranges between min-range 3 and max-range 7, he can only convert if the currency value is between 3 and 7.
There will be converters for several types of currency, which is why I'm using a second "range function" and variables for "inputtext" etc.
Most of it seem to work, as the "range" function seem to pick up the min-range, max-range and answer values etc. But I cant find a way to return it back to the currency_convert(). I've tried changing the values in range() to zero before trying to set new values for them, lists, loops and everything. Currency_convert() still shows the same currency value after trying to change it in range(). How do I send the "answer" back to currency_convert()? Do i have to make a "answer2" in range() or something?
Any ideas what I have done wrong and what to do?
This is how the part of the script looks like atm:
def range(min_range, max_range, answer, inputtext):
if answer > min_range and answer < max_range:
return
else:
answer = 0
min_range = input("Set a min range for conversion: ")
max_range = input("Set a max range for conversion: ")
answer = input(inputtext)
Return answer (?)
def currency_convert():
min_range = input("min range: ")
max_range = input("max range: ")
inputtext = "How many dollars to convert: "
answer = input(inputtext)
range(min_range, max_range, answer, inputtext)
sek = answer * 3
print("%d is the same as" % (sek))
Upvotes: 2
Views: 188
Reputation: 1228
You should return back all the required items (I guess min_range
, max_range
and answer
, since they are the ones gettings changed in your range()
function) and then accept them in your currency_convert()
function. Example -
def newrange(min_range, max_range, answer, inputtext):
if answer > min_range and answer < max_range:
return min_range, max_range, answer
else:
answer = 0
min_range = input("Set a min range for conversion: ")
max_range = input("Set a max range for conversion: ")
answer = input(inputtext)
return min_range, max_range, answer
def currency_convert():
min_range = input("min range: ")
max_range = input("max range: ")
inputtext = "How many dollars to convert: "
answer = input(inputtext)
min_range, max_range, answer = newrange(min_range, max_range, answer, inputtext)
sek = answer * 3
print("%d is the same as" % (sek))
I would also suggest not to name your function or variable as range
as that would mask the builtin function range()
.
Upvotes: 2
Reputation: 13790
First a simple answer. The call should be answer = adjust_range(min_range, max_range, answer, inputtext)
. As Sharon Dwilif K said, calling a function "range" isn't good because it shadows the builtin. Then the final statement in the function would simply be return answer
.
However it sounds like using a class could help. Here is a complete working example using a class:
class CurrencyConverter:
"""Convert currency within a certain range."""
RATES = {'USD_SEK': 3,
'USD_INR': 60}
def __init__(self, currency_from, currency_to):
self.currency_from = currency_from
self.currency_to = currency_to
self.min_range = 0
self.max_range = 0
self.amount_from = 0
def ask_range(self):
self.min_range = int(input("Set a min range for conversion: "))
self.max_range = int(input("Set a max range for conversion: "))
def calculate_amount_to(self):
self.ask_amount_from()
self.adjust_range()
rate_key = '%s_%s' % (self.currency_from, self.currency_to)
return self.amount_from * self.RATES[rate_key]
def ask_amount_from(self):
self.amount_from = int(
input("How many %s to convert: " % self.currency_from))
def adjust_range(self):
"""Makes sure the self.amount_from is within the range."""
if not self.min_range <= self.amount_from <= self.max_range:
self.ask_range()
self.ask_amount_from()
if __name__ == "__main__":
converter = CurrencyConverter('USD', 'SEK')
converter.ask_range()
sek = converter.calculate_amount_to()
print("%d is the result in SEK." % sek)
sek = converter.calculate_amount_to()
print("%d is another result in SEK." % sek)
converter = CurrencyConverter('USD', 'INR')
converter.ask_range()
inr = converter.calculate_amount_to()
print("%d is rupees." % sek)
Upvotes: 2