Saturday, September 12, 2026

Week 16 Notes



# 
# Python Problem Solver
# Week 16 Example 1: Reverse the digits
#

with open("reverse.txt","r") as f:
    l = list(map(int,f.read().strip().split()))
    for n in l:
        while n > 0:
            print(n % 10, end = "")
            n //= 10
        print(" ", end = "")
    print()



# 
# Python Problem Solver
# Week 16 Example 2: Octal numbers
#

with open("octal.txt","r") as f:
    l = list(map(int,f.read().strip().split()))
    for n in l:
        value = 0
        while n > 0:
            value *= 8
            value += n%10
            n//= 10
        print(value, end=" ")
    print()



# 
# Python Problem Solver
# Week 16 Example 3: API
#
import json, urllib.request
#This free API key gives you restricted access to some of the data using the thesportsdb.com API
API_Key = "123"
name = input("Enter a player's name:").title()
name = name.replace(' ', '_')
url = "https://www.thesportsdb.com/api/v1/json/" + API_Key + "/searchplayers.php?p=" + name
response = urllib.request.urlopen(url)
result = json.loads(response.read())
for i in range(0, len(result["player"])):
  print(result["player"][i]["strPlayer"] + " - " + result["player"][i]["strTeam"])

No comments:

Post a Comment

Term 2 Term Test