Question

Assignment Overview This assignment will give you more experience on the use of strings and iterations....

Assignment Overview

This assignment will give you more experience on the use of strings and iterations. The goal of this project is to use Google’s currency converter API to convert currencies in Python and display the result to user by processing the returned results.

Assignment Background

The acronym API stands for “Application Programming Interface”. It is usually a series of functions, methods or classes that supports the interaction of the programmer (the application developer, i.e., you) with some particular program. Google has several API's available for anyone to use. To interact with Google’s currency converter API, you have to create a GET request which is basically a URL and some values inserted in specific places. Our goal is to prompt the user for an amount, original currency and target currency, then setup the URL and request Google currency converter to convert it. We then process the returned value to extract the result, then display it to the user.

Project Description

Your program must meet the following specifications: 1. At program start, prompt the user for the original currency. 2. Then prompt the user for the target currency. 3. Next prompt for the amount which must be an int. 4. Setup the URL as explained below and send the request. 5. Convert the returned value to string, and then process it to find the converted value. 6. Display the result to the user. 7. Prompt the user if they want to repeat.

Set up URL

A sample URL is https://finance.google.com/finance/converter?a=100&from=USD&to=EUR You can put that link in a browser and see how Google currency converter converts 100 USD to EUR (convert $100 US to Euros). To change the amount or the currencies, you can simply replace those values in the URL by the values entered by the user. For this project, you can assume the user’s currency input is always correct. You will need to check that the amount to be converted is correct. The URL needs to be a string for use in the urlopen(url) call below. You need to construct a URL string that has a value (such as 100 in sample URL above), the currency you are converting from (such as USD in the sample URL above) and the currency you are converting to (such as EUR in the sample URL above). You will be prompting for those three values so you need to insert them into your URL string. How do you do that? In Python we have the string format statement described in Section 4.4 of the text. Usually it is use inside a print()function, but it doesn’t have to be. Consider this code:

some_str = 'XYZ'

some_int = 123

a_str = "abc{:d}def{:s}".format(some_int,some_str)

print(a_str) # prints abc123defXYZ

Process Results

Your project should start by importing urllib.request. You can use response = urllib.request.urlopen(url) to do the conversion. Then read the response and convert it to string by result = str(response.read()). To find the converted value in the result string, you can find that appears right before the value we are looking for and that appears right after that (Hint: we said find as a hint to use the string find() method on the result string). You can then extract the value by slicing the result string using the indices you found.

Input Value Checking

The value input must be an integer. In a non-integer is input, you must re-prompt for the value (not reprompt for the currency names). Hint: use a while loop (because you don’t know how many times an incorrect value will be input, and the string isdigit() method is useful to checking if an input string is all digits, i.e. is an integer.

Output Value

The value output must be a decimal with two decimal places

import urllib.request

# Strings:
#"What is the original currency? "
#"What currency do you want to convert to? "
#"How much do you want to convert (int)? "
#"The value you input must be an integer. Please try again."
#"Do you want to convert another currency? "
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#currencyConverter.py

import urllib.request
while 1:
    #getting the currency
    original_currency=(input('What is the original currency? '))
    convert_to_currency=input("What currency do you want to convert to? ")
    amount=0
    #validating and taking amount
    while 1:
        try:
            amount=int(input("How much do you want to convert (int)? "))
            break
        except Exception as e:
            print("The value you input must be an integer. Please try again.")
    #constructing the url
    url='https://finance.google.com/finance/converter?a={:d}&from={:s}&to={:s}'.format(amount,original_currency,convert_to_currency)
   #getting the response
    response=urllib.request.urlopen(url)
    line=""
   #searching for line that contains converted result
    for i in response.readlines():
        i=str(i)
        #this is the div id that contains result
        if 'id=currency_converter_result' in i:
            line=i
            break
    #processing the string , first splitting vy >
    line=line.strip().split('>')
    #in case of bad request
    if len(line)<4:
        print("Bad request , currency mismatch")
    else:
        #printing the result
        converted_currency=line[2].split('<')[0]
        #if converted in decimal then rounding it to 2 decimal places
        if '.' in converted_currency:
            #first splited the string by " "
            converted_currency=converted_currency.split()
            #converting to float and rounding
            converted_currency[0]=round(float(converted_currency[0]),2)
            #again converted to string
            converted_currency=str(converted_currency[0])+" "+converted_currency[1]
      #  print(amount)
       # print(original_currency)
        #print(converted_currency)
        print("{:d} {:s} = {:s}".format(amount,original_currency,converted_currency))
    #getting response for next conversion
    ans=input('Do you want to convert another currency?(Y/N)  ').lower()
    if ans=='n' or ans == 'no':
        break

#OUTPUT

What is the original currency? INR What currency do you want to convert to? EUR How much do you want to convert (int)? 12 12

Please do let me know if u have any concern...

Add a comment
Know the answer?
Add Answer to:
Assignment Overview This assignment will give you more experience on the use of strings and iterations....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you...

    Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you some experience with writing functions that take in arguments and return values, as well as writing a program that consists of multiple files. This assignment also requires the use of a switch statement, in addition to more practice with printf& scanf declaring variables, using loops, and using logical expressions. For this assignment, you will prompt the user in the main function to enter their...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention...

    C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention to the rules at the bottom. Extra good rating to whoever can help me with this. TIA. In certain programming situations, such as getting information from web forms, via text boxes, the data coming in may need to be numerical (we want to perform arithmetic on it), but it is stored in the form of a list of characters (the numerical value stored is...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Overview This assignment will give you experience on the use of classes. Understand the Application Every...

    Overview This assignment will give you experience on the use of classes. Understand the Application Every internet user–perhaps better thought of as an Internet connection–has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other—sometimes optional, sometimes needed—data (such as a MAC address, port, local IP address, gateway, etc), but we keep things simple and use...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Please write this in Java, please write comments as you create it, and please follow the...

    Please write this in Java, please write comments as you create it, and please follow the coding instructions. As you are working on this project, add print statements or use the debugger to help you verify what is happening. Write and test the project one line at a time. Before you try to obtain currency exchange rates, obtain your free 32 character access code from this website: https://fixer.io/ Here's the code: 46f27e9668fcdde486f016eee24c554c Choose five international source currencies to monitor. Each...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT