Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # conditional statements today!
- print("Welcome!")
- # NEW FILE
- age = input("Give your age:\n")
- age = int(age)
- # if/else - if age is larger than 20 or if it's something else
- if age > 20:
- print("You're over 20 years old!")
- print("We're inside the if-statement now!")
- else:
- print("You're 20 years or less years old.")
- print("Thank you for using the application!")
- # NEW FILE
- age = input("Give your age:\n")
- age = int(age)
- # if/else - if age is larger than 20 or if it's something else
- if age < 20:
- print("You're less than 20 years old!")
- elif age < 30:
- print("You're less than 30 years old!")
- else:
- print("You're over 30 years old.")
- print("Thank you for using the application!")
- # NEW FILE
- age = input("What's your age again?\n")
- age = int(age)
- # is age less or equal to 30
- if age <= 30:
- print("30 or less years old")
- # is age greater than 30
- if age > 30:
- print("More than 30 years old")
- # is age EXACTLY 30
- if age == 30:
- print("You're exactly 30 years old")
- # is age something else than EXACTLY 30
- if age != 30:
- print("You're not exactly 30 years old.")
- # NEW FILE
- # two number variables for later comparison
- number1 = input("Give a number:\n")
- number1 = int(number1)
- number2 = 123
- # which variable has a bigger value?
- if number1 > number2:
- print("First number is bigger!")
- else:
- print("Second number is bigger!")
- # NEW FILE
- # does the user know the secret word
- secret_word = "banana"
- text = input("Give us a word:\n")
- # just like any variable, comparison
- if text == secret_word:
- print("SECRET WORD IS BANANA!")
- else:
- print("You don't know the secret word...")
- # NEW FILE
- text = input("Which drink you want?\n")
- # react to different user answers
- if text == "milk":
- print("Milk is 1.5 € per liter")
- elif text == "coffee":
- print("Coffee is 7 € per 500g")
- elif text == "water":
- print("Just use the tap water, will you?")
- else:
- print("We don't have that drink, SORRY!")
- # NEW FILE
- choice = input("Are you a student? (y/n)\n")
- # did user choose y or n
- # we could use else, but then "banana" would also result to "n"
- if choice == "y":
- print("You're a student!")
- print("You get a 50% discount from train ticket!")
- elif choice == "n":
- print("Not a student, full price + postage!")
- print("Thank you!")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # if the remainder when dividing by 2 is 0 ==> Even number
- # otherwise, odd number
- if number % 2 == 0:
- print("EVEN NUMBER")
- else:
- print("ODD NUMBER")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # preferred way for this is:
- # if 0 < number < 30:
- if number > 0 and number < 30:
- print("Number is between 0 and 30")
- if number < 0 or number > 30:
- print("Number is outside 0 and 30")
- # NEW FILE
- age = 20
- city = "London"
- student = True
- # this if-statement is only run if ALL OF THE variables are correct
- # in the condition!
- if age >= 18 and city == "Rovaniemi" and student == True:
- print("Adult, student, from Rovaniemi.")
- print("Thank you!")
- # NEW FILE
- # ask integer from user
- age = input("Age:\n")
- age = int(age)
- # text is easy, just use input
- city = input("City:\n")
- # with booleans we have to control the
- # variable ourselves with an if-statement
- student = True
- choice = input("Student? (y/n):\n")
- # change boolean to False if not a student
- # else is not needed, because the default value
- # of student is already True
- if choice == 'n':
- student = False
- # this if-statement is only run if ALL OF THE variables are correct
- # in the condition!
- if age >= 18 and city == "Rovaniemi" and student == True:
- print("Adult, student, from Rovaniemi.")
- print("Thank you!")
- # NEW FILE
- # ask integer from user
- age = input("Age:\n")
- age = int(age)
- # text is easy, just use input
- city = input("City:\n")
- # with booleans we have to control the
- # variable ourselves with an if-statement
- student = True
- choice = input("Student? (y/n):\n")
- # change boolean to False if not a student
- # else is not needed, because the default value
- # of student is already True
- if choice == 'n':
- student = False
- # nested if statements. the city is only checked if age is more than 18
- if age >= 18:
- print("Adult!")
- if city == "Rovaniemi":
- print("Health care address: Test Alley 12")
- if city == "Helsinki":
- print("Health care address: Business Street 5 A")
- else:
- print("Health care for underage people, ask your school.")
- print("Thank you!")
- # NEW FILE
- status = "student"
- price = 26
- # different price logic for students and other customers
- # students get 50% discount, others have full price + 2.5€ service fee
- if status == "student":
- print("Student price is 50%, minimum price is 15€")
- price = price * 0.5
- # fix the price if it goes under 15 €
- if price < 15:
- price = 15
- elif status == "other":
- print("Other customers, full price + 2.5€ service fee")
- price = price + 2.5
- # print the price
- print(f"Price: {price} €")
- # NEW FILE
- temperature = input("Temperature?\n")
- temperature = int(temperature)
- # initalize this variable with some default value
- # so it doesn't crash in the else-statement
- is_hot = False
- if temperature > 20:
- is_hot = True
- print(temperature)
- else:
- print(temperature)
- print(is_hot)
- # NEW FILE
- # situation outside
- humidity = 90
- temperature = -5
- raining = False
- # if humidity is large, it's raining
- if humidity > 80:
- raining = True
- # ... but if temperature is below zero
- # it's not rain anymore, it's hailing
- if temperature < 0:
- raining = False
- # HERE COULD BE HUNDREDS OF LINES OF OTHER CODE
- # final check, is it raining or not?
- if raining == True:
- print("It's raining!")
- else:
- print("Not raining.")
- # NEW FILE
- # MORE COMPLEX BOOLEAN EXAMPLE
- # OUR APPLICATION'S LOGIC:
- # we have to determine, if weather outside is bad or not
- # bad weather has temperature less than 10
- # bad weather has humidity more than 80
- # bad weather has wind speed more than 2.5
- # bad weather: it is dark outside
- # it's dark outside, if the time is between 20-24 or 0-7
- # initalize variables, you can replace these with input too
- temperature = 8
- humidity = 88
- wind_speed = 5
- sun_goes_down = 20
- sun_rises = 7
- current_hour = 15
- # assume it's a good weather in the beginning
- good_weather = True
- # without booleans, this kinds of ridiculous conditional stuff happens
- # if temperature < 10 and humidity > 80 and wind_speed > 2.5 and (current_hour > sun_goes_down or ...)
- # temperature check
- if temperature < 10:
- good_weather = False
- # humidity check
- if humidity > 80:
- good_weather = False
- # wind speed cheeck
- if wind_speed > 2.5:
- good_weather = False
- # time of day check
- if current_hour >= sun_goes_down or current_hour <= sun_rises:
- good_weather = False
- # final decision, is it a good weather outside?
- if good_weather:
- print("It's a good weather outside!")
- else:
- print("It's a bad weather... :(")
Advertisement
Comments
-
- Brilliant work, you! ;)
- gotta start somewhere, I suppose hehe
Add Comment
Please, Sign In to add comment
Advertisement