Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Calculator {
add(a, b) {
return a + b;
}

subtract(a, b) {
return a - b;
}

multiply(a, b) {
return a * b;
}

divide(a, b) {
if (b === 0) {
throw new Error("Division by zero is not allowed.");
}
return a / b;
}
}

// Example usage:
const calc = new Calculator();
console.log(calc.add(6, 3)); // Output: 8
console.log(calc.subtract(5, 3)); // Output: 2
console.log(calc.multiply(5, 3)); // Output: 15
console.log(calc.divide(5, 3)); // Output: 1.6666666666666667
6 changes: 3 additions & 3 deletions card_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import itertools, random

# make a deck of cards
deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club'])
deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

# shuffle the cards
random.shuffle(deck)

# draw five cards
print("You got:")
for i in range(5)
print(deck[i][0], "of", deck[i][1]
for i in range(5):
print(deck[i][0], "of", deck[i][1])
1 change: 1 addition & 0 deletions helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello, World!")
49 changes: 26 additions & 23 deletions sum_elements.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
#A poorly written example of a program in Python. It prompts the user for the number of elements to sum, takes those integers as input, and handles some basic error cases

MAX = 100

def calculate_sum(arr):
result = 0
for num in arr:
result += num
return result
return sum(arr)

def get_number_of_elements():
while True:
try:
n = int(input("Enter the number of elements (1-100): "))
if 1 <= n <= MAX:
return n
else:
print("Invalid input. Please provide a number ranging from 1 to 100.")
except ValueError:
print("Invalid input. Please enter a valid integer.")

def get_elements(n):
arr = []
print(f"Enter {n} integers:")
for _ in range(n):
while True:
try:
arr.append(int(input()))
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
return arr

def main():
try:
n = int(input("Enter the number of elements (1-100): "))
if not 1 <= n <= MAX:
print("Invalid input. Please provide a digit ranging from 1 to 100.")
exit(1)

arr = []

print(f"Enter {n} integers:")
for _ in range(n):
try:
arr.append(int(input()))
except ValueError:
print("Invalid input. Please enter valid integers.")
exit(1)

n = get_number_of_elements()
arr = get_elements(n)
total = calculate_sum(arr)

print("Sum of the numbers:", total)

except KeyboardInterrupt:
print("\nProgram terminated by user.")
exit(1)
Expand Down
21 changes: 21 additions & 0 deletions weather_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import requests

def get_weather(city_name, api_key):
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "q=" + city_name + "&appid=" + api_key
response = requests.get(complete_url)

if response.status_code == 200:
data = response.json()
main = data['main']
weather = data['weather'][0]
print(f"City: {city_name}")
print(f"Temperature: {main['temp']}K")
print(f"Weather: {weather['description']}")
else:
print("City not found.")

if __name__ == "__main__":
city_name = input("Enter city name: ")
api_key = "your_api_key_here" # Replace with your actual API key
get_weather(city_name, api_key)