Commit 61e13781 authored by Raquel's avatar Raquel 💬

week3/ day1 day2

parent 5464e298
.idea
.DS_Store
\ No newline at end of file
.idea
.DS_Store
\ No newline at end of file
print("Let’s calculate your BMI (kg/m2)")
user_w_input= input("what is your weight(in Kg)? ")
user_h_input=input("what is your height(in cm)?")
def user_BMI(user_w_input,user_h_input ):
user_weight = float(user_w_input)
user_height = int(user_h_input)*0.001 #to go from cm to m
BMI=user_weight/user_height*user_height
output = " you are "
if BMI<18.5:
output = "underweight"
elif BMI>18.5 and BMI<24.9 :
output += "average"
elif BMI > 25 and BMI < 29.9:
output +="overweight"
elif BMI > 30 and BMI < 34.9:
output += "obese"
elif BMI > 35:
output += "extremely obese"
return output
print(user_BMI (user_w_input,user_h_input))
def dict(n):
a= {x: x*x for x in range(1,n) }
return a
print(dict(10))
# for n=10:
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
\ No newline at end of file
import math
import random
user_input=int(input("I am thinking of a number from \'1-10\'. Can you find it? You have 5 tries." ))
def start(user_input):
number=random.randint(1,11)
print(number)
print(user_input)
count = 1
while user_input != number :
if user_input > number:
count += 1
user_input=int(input("it's lower than that. try again"))
elif user_input < number:
count += 1
user_input=int(input("it's higher than that. try again"))
else:
return count
print("congrats you guessed it in " + str(count) + " tries.")
user_input_end=input("Do you want to play again? Y/N" )
play_again(user_input_end)
def play_again(user_input_end):
if user_input_end=="Y":
user_input_new = int(input("I am thinking of a number from \'1-10\'. Can you find it? You have 5 tries."))
start(user_input_new)
elif user_input_end=="N":
print("goodbye!")
print(start(user_input))
shopping_list = []
def add_item():
global shopping_list #to allow for changes to the global variable
user_add=input("what do you want to add?" )
user_position = input("what list position do you want to add your item in? input \'a\' if any or a position number")
if user_position.lower() == "a": #to make it case-insensitive
shopping_list.append({len(shopping_list)+1 : user_add})
elif type(int(user_position))== int :
shopping_list.insert(int(user_position)-1, {int(user_position): user_add})
# WIP - to update the keys numbers after removal
#WIP - code not going into the else
else:
print("please try again")
add_item()
return shopping_list
def remove_item():
global shopping_list #to allow for changes to the global variable
user_remove=input("what do you want to remove? Please input the item number. " )
del shopping_list[int(user_remove)-1]
# WIP - to update the keys numbers after removal
"""
for item in shopping_list:
for key in item.keys():
if key > int(user_remove):
key -= 1
"""
return shopping_list
def show_items():
global shopping_list
print(*shopping_list)
def help_menu():
print("Press \'h\' for help menu")
print("Press \'s\' to show the item in your list")
print("Press \'a\' to add a new item to the list")
print("Press \'r\' to remove an item from the list")
print("Press \'q\' to quit")
while True:
user_to_do=input("What do you want to do?" )
if user_to_do == "a":
add_item()
elif user_to_do == "r":
remove_item()
elif user_to_do == "s":
show_items()
elif user_to_do == "h":
help_menu()
elif user_to_do == "q":
break
print("goodbye")
print(help_menu())
\ No newline at end of file
array=[1,2,3,4,5,6,7,8,9,10]
my_filter= list(filter( lambda x: x % 2 == 0, array))
print(my_filter)
#Write a function find_numbers(min, max) that will find all numbers that are a multiple of 7 but not a multiple of 5.
def find_numbers(min,max):
result=[]
for x in range(min,max):
if x%7==0 and x%5!=0:
result.append(x)
return result
print(find_numbers(2,100))
#second way to do it just with filter and lambda
def find_numbers2(min,max):
return list(filter(lambda x: (x%7==0 and x %5!=0), range(min,max)))
print(find_numbers2(2,100))
\ No newline at end of file
squares_list=[x*x for x in range(1,21)]
print_list= squares_list[len(squares_list)-5:len(squares_list):]
print(print_list)
# [256, 289, 324, 361, 400]
\ No newline at end of file
orders = [
{
'id': 'order_001',
'item': 'Introduction to Python',
'quantity': 1,
'price_per_item': 32,
},
{
'id': 'order_002',
'item': 'Advanced Python',
'quantity': 3,
'price_per_item': 40,
},
{
'id': 'order_003',
'item': 'Python web frameworks',
'quantity': 2,
'price_per_item': 51,
},
]
def compute_totals(orders):
totals = []
for order in orders:
#print(order) --to chekc it accesses the orders
if order["quantity"]* order["price_per_item"] <100:
order["total"]=order["quantity"]* order["price_per_item"] +10
totals.append({order["id"], order["total"]})
else:
order["total"] = order["quantity"] * order["price_per_item"]
totals.append({order["id"], order["total"]})
return totals
print(compute_totals(orders))
print_style= [x if x%2==0 else "_" for x in range(1,11)]
print(*print_style)
\ No newline at end of file
array=[1,2,3,4,5,6,7,8,9,10]
my_square= list(map(lambda x: x*x, array))
print(my_square)
\ No newline at end of file
array=[1,2,3,4,5,6,7,8,9,10]
squared= map(lambda x: x*x, array)
squared_even=list(filter(lambda x: x%2==0, squared))
print(squared_even)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment