-1

I have been asked to make conversion system, the conversion doesn't have to be correct. I need help with only this following task.

Task 2: The function get_amount_to_convert() needs to be completed. • It should ask the user for an amount in GBP they would like to convert • If the amount is not a numerical value it should output that a numerical value is expected and ask for a correct value to be input. • Otherwise it should return float(amountToConvert) Test your work by running the script. If it works then the value that is entered should be displayed in the text at the bottom of the screen.

This is currently all i need to do so there's no need to help with the rest.

'''
travelagent.py

##########################################
####  Travel Agency ####
##########################################

Bureau de Change system to convert from GBP 
to requested currency.

Suported currencies: GBP,USD,EUR,BRL,JPY,TRY

Version 0.01 
Author: 
email:
 
'''
#set up some dictionaries to store data - leave alone
exchangeRate = {
    #"GBP":0,
   "USD":1.40,
    "EUR":1.14,
    "BRL":4.77,
    "JPY":151.05,
    "TRY":5.68
}

#set up some variables for general use
baseTransactionFee = 0.35   #percentage transaction fee for upto 300GBP 
staffDiscountRate = 0.05     #percentage discount rate to apply to staff discounts
staffMember = False              #Initial status of staff member discount
transactionFee = 0              #Initial status of transactionFee

def welcome_message(): #get amount to convert
  print("Hello\nWelcome to the Bureau De Change System")

def get_amount_to_convert():
   amountToConvert = input("How much would you like to convert from GPB? ")
   if amountToConvert == str:
     print("That is not a valid amount, please input a number")
     get_amount_to_convert
     
   elif amountToConvert != str:
    return float(amountToConvert)
    
get_amount_to_convert()

def get_currency_to_convert_to():
  currencyToConvertTo = "usd"
  return currencyToConvertTo

def is_staff_member(): #find out if this is for a staff member
  return True

def convert_currency(currencyToConvertTo,amountToConvert):
  convertedAmount = 0
  return convertedAmount

def work_out_the_fee(transactionFee,amountToConvert):#Work out the fee
  if amountToConvert <= 5:
    transactionFee = transactionFee+(amountToConvert*0.035)
  return transactionFee

def work_out_total_cost(transactionFee,amountToConvert): #workout the total cost
  totalCost = 0
  return totalCost


# Leave the code below alone  **NO NEED TO EDIT IT** :)

def print_result(amountToConvert,currencyToConvertTo,convertedAmount,transactionFee,totalCost,staffMember):
  print("\nThe conversion is as follows:\n")
  print("Converting %s GPB to %s results in an amount of %s %s" %(str(format(amountToConvert,'.2f')),currencyToConvertTo,str(format(convertedAmount,'.2f')),currencyToConvertTo,))
  print("The transaction fee for this is %s GBP" % (str(format(transactionFee,'.2f')),))

  if staffMember == True:
    discountAmount = transactionFee * staffDiscountRate
    print("A staff discount of %s GBP will also be applied" %(str(format(discountAmount,'.2f'))))
    totalCost = totalCost - discountAmount

  print("\nThis results in a total charge of %s GBP" % format(totalCost,'.2f'))

welcome_message()

amountToConvert = get_amount_to_convert()

targetCurrency = get_currency_to_convert_to()

staffMember = is_staff_member()

convertedAmount =convert_currency(targetCurrency,amountToConvert)

transactionFee = work_out_the_fee(transactionFee,amountToConvert)

totalCost = work_out_total_cost(transactionFee,amountToConvert)

print_result(amountToConvert,targetCurrency,convertedAmount,transactionFee,totalCost,staffMember)
xurvx
  • 1
  • 1
  • What is `str`, the built-in type? What is `get_amount_to_convert`? Can you be a little clearer what your expected output/behaviour is, and how what you are getting differs from that? – L.Grozinger Oct 23 '20 at 16:05
  • I'm pretty new to python but i have used str because i thought the input from the user would be seen as a string if they input words. i could be completely wrong sorry if this didnt answer your question properly – xurvx Oct 23 '20 at 16:08
  • ah ok then I should say that you can use `isinstance` to test the types of things, look at the docs here https://docs.python.org/3/library/functions.html#isinstance – L.Grozinger Oct 23 '20 at 16:11

2 Answers2

0

something like this should help:

def get_amount_to_convert():
   amountToConvert = input("How much would you like to convert from GPB? ")
   try:
       return float(amountToConvert)
   except:
       print("this is not a valid value, please enter a numeric value")
       get_amount_to_convert()
    
get_amount_to_convert()
Seth
  • 2,214
  • 1
  • 7
  • 21
Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • just showing how he can test it first but you're right returning it immediately is more practical – Ironkey Oct 23 '20 at 16:09
  • this works but if i enter a number after entering a letter previously it takes me to the welcome message, would it be helpful if i shared the entire code so you could get a better understanding? – xurvx Oct 23 '20 at 16:23
  • yes, it would, we assumed that you wanted the user to input another value if they give a wrong one. – Ironkey Oct 23 '20 at 16:24
  • i have updated it, it is for my school work and was laid out so most of the work isn't complete, its just task 2 which i'm currently stuck on – xurvx Oct 23 '20 at 16:37
  • This doesn’t return anything from a recursive call. – deceze Oct 23 '20 at 17:15
0

The variable amountToConvert is not exactly the same as the built-in str. Try using a try/except.

def get_amount_to_convert():
   try:
      return float(input("How much would you like to convert from GPB? "))
   except ValueError:
     print("That is not a valid amount, please input a number")
     return get_amount_to_convert()
Seth
  • 2,214
  • 1
  • 7
  • 21