Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 3, conditional statements!
- print("Welcome!")
- # NEW FILE
- from decimal import Decimal
- # this is a somewhat tedious feature of the float data type
- number1 = float(0.1)
- number2 = float(0.2)
- print(f"Normal float/decimal numbers: {number1} + {number2} =")
- print(number1 + number2)
- # 0.1 is a problematic float value, or pretty much all values that are between 0 and 0.1
- # this happens because of the method how computers handle floating numbers
- # basically this is a problem in each programming languages, however, some programming languages "take care of this"
- # automatically for the programmer
- # easy fix usually: round the value
- total = number1 + number2
- total = round(total, 1)
- print(total)
- # one way is to use the decimal-module in Python
- # note: remember to import decimal
- number3 = Decimal("0.1")
- number4 = Decimal("0.2")
- print(f"Decimal numbers of the decimal-module: {number3} + {number4} =")
- print(number3 + number4)
- # NEW FILE
- # user's age
- age = 19
- # conditional statement -> is age under 20?
- # if it is -> run the print-code below
- if age < 20:
- print("You are less than 20 years old.")
- print("This is also only run if the condition is true.")
- print("This is run regardless of the if-statement, since it has no indentation.")
- # NEW FILE
- # ask user's age (integer)
- age = input("How old are you?\n")
- age = int(age)
- # is the user less than 20 years old?
- # if yes => print "You're less than 20 years old."
- # if not => go to the else -statement
- if age < 20:
- print("You're less than 20 years old.")
- else:
- print("You're at least 20 years old.")
- print("Thank you for using the application!")
- # NEW FILE
- # ask user's age (integer)
- age = input("How old are you?\n")
- age = int(age)
- # is the user less than 20 years old?
- # if yes => print "You're less than 20 years old."
- # if not => go to the else -statement
- if age < 20:
- print("You're less than 20 years old.")
- elif age < 30:
- print("You're less than 30 years old.")
- elif age < 40:
- print("You're less than 40 years old.")
- else:
- print("Your age is something else.")
- print("Thank you for using the application!")
- # NEW FILE
- # ask user's age (integer)
- age = input("How old are you?\n")
- age = int(age)
- # ask month number
- month = input("Which month you wish to visit the store?\n")
- month = int(month)
- # is the user less than 20 years old?
- # if yes => print "You're less than 20 years old."
- # if not => go to the else -statement
- if age < 20:
- print("You're less than 20 years old.")
- elif age < 30:
- print("You're less than 30 years old.")
- elif age < 40:
- print("You're less than 40 years old.")
- else:
- print("Your age is something else.")
- # separate if-statement for month-number
- if month == 7:
- print("We're closed on July.")
- print("Back open in August, welcome!")
- print("Thank you for using the application!")
- # NEW FILE
- price = 200
- age = 21
- # if user is under 18 years old
- if age < 18:
- # replace the price with the old price * 0.9 (10% discount)
- price = price * 0.9
- else:
- # for customers 18 years old or more,
- # price is the old price + 4.95 postage
- price = price + 4.95
- # since we are modifying the price only in the if/else
- # print out the price whatever it is now!
- print(f"Total sum: {price} €")
- # NEW FILE
- age = input("How old are you?\n")
- age = int(age)
- # less than 30
- if age < 30:
- print("Less than 30 years old.")
- # 30 or more
- if age >= 30:
- print("30 years old or more.")
- # is age exactly 30 => use two == signs
- # because one = means creating a variable
- if age == 30:
- print("Exactly 30 years old.")
- # is age something else than exactly 30
- if age != 30:
- print("You're not exactly 30 years old.")
- # NEW FILE
- # ask one number from user
- # remember always to convert your input into numeric format
- # because otherwise you compare text to numbers
- number1 = input("Give a number:\n")
- number1 = int(number1)
- number2 = 234
- # if one of these numbers is still text format
- # (no int/float used), this can be very buggy
- if number1 > number2:
- print("The first number is bigger!")
- else:
- print("The second number is bigger")
- # NEW FILE
- # ask user a sales code
- sales_code = input("Give your discount code:\n")
- current_code = "WINTER24"
- # for example if "FALL23" == "WINTER24"
- # we can't use > or < with text, since
- # if "banana" > "firetruck" -> doesn't make any sense
- if sales_code == current_code:
- print("Discount to price, -20%!")
- else:
- print("Normal price.")
- # NEW FILE
- # ask user the product
- drink = input("What would you like to drink?\n")
- # if-elif-else -> structure
- # -> react to different user responses
- # else -> drink not found
- if drink == "milk":
- print("Price of milk: 1 €")
- elif drink == "coffee":
- print("Price of coffee: 7 €")
- elif drink == "water":
- print("Free, drink from the tap.")
- else:
- print("Drink not found.")
- # NEW FILE
- # ask the user if they are student or not
- choice = input("Are you a student? (s/o)\n")
- # if is student
- if choice == "s":
- print("This code only considers students")
- print("For example, calculate ticket price etc.")
- elif choice == "o":
- print("Calculate price for other customers etc.")
- else:
- print("Incorrect selection. Please restart the application.")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # if remainder of the number when dividing by 2
- # is exactly zero => even number
- # if not (else) => odd number
- if number % 2 == 0:
- print("Even number")
- else:
- print("Odd number")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # is number between 0 and 30, or in the range 0-30
- if number > 0 and number < 30:
- print("Number is in the range 0-30")
- # is number less than 0 OR more than 30
- if number < 0 or number > 30:
- print("Number is less than 0, or more than 30")
- print("Basically, this is the opposite of the previous statement.")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # is number between 0 and 30, or in the range 0-30
- # this is the recommended shorthand in Python
- # for numeric range checks
- if 0 <= number <= 30:
- print("Number is in the range 0-30")
- # NEW FILE
- year = input("Give a year (2000-2012)\n")
- year = int(year)
- # easy to compare a bunch of range values
- # always double-check all comparisons
- # and try your with test values
- # ESPECIALLY THE BOUNDARY VALUES
- # for example, 2004, 2008, 2012
- if 2000 <= year < 2004:
- print("2000-2004")
- elif 2004 <= year < 2008:
- print("2004-2008")
- elif 2008 <= year < 2012:
- print("2008-2012")
- else:
- print("Incorrect year.")
- # NEW FILE
- age = 20
- city = "Rovaniemi"
- student = True
- # condition that is depending on three different variables
- if age >= 18 and city == "Rovaniemi" and student == True:
- print("An adult student from Rovaniemi")
- print("If for some reason you need to have a very specific condition")
- print("For example, marketing or information purposes, you can do this.")
- print("Thank you for using our application.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement