0% found this document useful (0 votes)
21 views5 pages

PYTHON Practice 4

The document outlines three programming tasks: creating a text output program using a specified font with pyfiglet, implementing a guessing game that prompts the user for a number and provides feedback on their guesses, and developing a Bitcoin price index program that fetches the current price of Bitcoin from an API and calculates the cost based on user input. Each task includes specific requirements for user input, error handling, and output formatting. Hints and installation instructions for necessary libraries are also provided.

Uploaded by

osman.pinnacle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

PYTHON Practice 4

The document outlines three programming tasks: creating a text output program using a specified font with pyfiglet, implementing a guessing game that prompts the user for a number and provides feedback on their guesses, and developing a Bitcoin price index program that fetches the current price of Bitcoin from an API and calculates the cost based on user input. Each task includes specific requirements for user input, error handling, and output formatting. Hints and installation instructions for necessary libraries are also provided.

Uploaded by

osman.pinnacle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Frank, Ian and Glen’s Leters

In a file called figlet.py, implement a program that:

• Expects zero or two command-line arguments:


o Zero if the user would like to output text in a random font.
o Two if the user would like to output text in a specific font, in which case the
first of the two should be -f or --font, and the second of the two should be
the name of the font.
• Prompts the user for a str of text.
• Outputs that text in the desired font.

If the user provides two command-line arguments and the first is not -f or --font or the
second is not the name of a font, the program should exit via sys.exit with an error
message.

Hints

• You can install pyfiglet with:


• pip install pyfiglet
• The documenta�on for pyfiglet isn’t very clear, but you can use the module as follows:
• from pyfiglet import Figlet

• figlet = Figlet()

You can then get a list of available fonts with code like this:

figlet.getFonts()

You can set the font with code like this, wherein f is the font’s name as a str:

figlet.setFont(font=f)

And you can output text in that font with code like this, wherein s is that text as a str:

print(figlet.renderText(s))

• Note that the random module comes with quite a few func�ons, per
docs.python.org/3/library/random.html.
Guessing Game
I’m thinking of a number between 1 and 100…

What is it?

It’s 50! But what if it were more random?

In a file called game.py, implement a program that:

• Prompts the user for a level,

• . If the user does not input a posi�ve integer, the program should prompt again.
• Randomly generates an integer between 1 and

• , inclusive, using the random module.

• Prompts the user to guess that integer. If the guess is not a posi�ve integer, the program should
prompt the user again.

• If the guess is smaller than that integer, the program should output Too small! and
prompt the user again.
• If the guess is larger than that integer, the program should output Too large! and prompt
the user again.
• If the guess is the same as that integer, the program should output Just right! and exit.

Hints

• Note that the random module comes with quite a few func�ons, per
docs.python.org/3/library/random.html.
Bitcoin Price Index
Bitcoin is a form of digi�al currency, otherwise known as
cryptocurrency. Rather than rely on a central authority like a bank,
Bitcoin instead relies on a distributed network, otherwise known as a
blockchain, to record transac�ons.
Because there’s demand for Bitcoin (i.e., users want it), users are willing to buy it, as by
exchanging one currency (e.g., USD) for Bitcoin.

In a file called bitcoin.py, implement a program that:

• Expects the user to specify as a command-line argument the number of Bitcoins,

• , that they would like to buy. If that argument cannot be converted to a float, the program
should exit via sys.exit with an error message.
• Queries the API for the CoinDesk Bitcoin Price Index at
htps://api.coindesk.com/v1/bpi/currentprice.json, which returns a JSON object, among whose
nested keys is the current price of Bitcoin as a float. Be sure to catch any excep�ons, as with code
like:
import requests

try:
...
except requests.RequestException:
...

Outputs the current cost of n Bitcoins in USD to four decimal places, using” , “as a thousands
separator.

Hints

• Recall that the sys module comes with argv, per


docs.python.org/3/library/sys.html#sys.argv.
• Note that the requests module comes with quite a few methods, per
requests.readthedocs.io/en/latest, among which are get, per
requests.readthedocs.io/en/latest/user/quickstart.html#make-a-request, and json, per
requests.readthedocs.io/en/latest/user/quickstart.html#json-response-content. You can
install it with:
• pip install requests
• Note that CoinDesk’s API returns a JSON response like:
• {
• "time":{
• "updated":"May 2, 2022 15:27:00 UTC",
• "updatedISO":"2022-05-02T15:27:00+00:00",
• "updateduk":"May 2, 2022 at 16:27 BST"
• },
• "disclaimer":"This data was produced from the CoinDesk Bitcoin
Price Index (USD). Non-USD currency data converted using hourly
conversion rate from openexchangerates.org",
• "chartName":"Bitcoin",
• "bpi":{
• "USD":{
• "code":"USD",
• "symbol":"$",
• "rate":"38,761.0833",
• "description":"United States Dollar",
• "rate_float":38761.0833
• },
• "GBP":{
• "code":"GBP",
• "symbol":"£",
• "rate":"30,827.6198",
• "description":"British Pound Sterling",
• "rate_float":30827.6198
• },
• "EUR":{
• "code":"EUR",
• "symbol":"€",
• "rate":"36,800.2764",
• "description":"Euro",
• "rate_float":36800.2764
• }
• }
• }
• Recall that you can format USD to four decimal places with a thousands separator with code
like:
• print(f"${amount:,.4f}")

You might also like