Thursday, October 29, 2015 8:25 PM

JavaScript If-else statements[BASICS]

PythonVery often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error. 
Example:
<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a time-based greeting:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var hour = new Date().getHours();
    var greeting;
    if (hour < 18) {
        greeting = "Good day";
    } else {
        greeting = "Good evening";
    }
    document.getElementById("demo").innerHTML = greeting;
}
</script>

</body>
</html>
Result:

Click the button to display a time-based greeting:

Python Conditional expression (Activity 4.7.1)

Python
Question:
Create a conditional expression that evaluates to string "negative" if user_val is less than 0, and "non-negative" otherwise. Example output when user_val is -9 for the below sample program:
-9 is negative
 
Code:
user_val = -9

cond_str =0
if user_val<0:
cond_str='negative'
else:
cond_str='non-negative'



print(user_val, 'is', cond_str) 

Python If-else expression (Activity 4.2.3)


PythonQuestion:
Write an expression that will print "in high school" if the value of user_grade is between 9 and 12 (inclusive). 

Code:
 user_grade = 10
if 9<=user_grade<=12:
    print('in high school')
else:
    print('not in high school')

Python Basic if-else expression (Activity 4.2.2)

Python
Question:
Write an expression that will print "Even" if the value of user_num is an even number. 

Code:

user_num = 13
if user_num%2==0:
    print('Even')
else:
    print('Odd') 

Python For Loops (Activity 4.1.2)

Python
Question:
Write an if-else statement for the following:
If user_tickets is less than 5, assign 1 to num_tickets. Else, assign user_tickets to num_tickets.
Ex: if user_tickets is 3, then num_tickets = 1. 

Code:
user_tickets = 0

if user_tickets<5:
    num_tickets=1
else:
    num_tickets=user_tickets

Wednesday, October 28, 2015 7:38 PM

Python Return (Activity 6.3.2)

Python
Question:
Define a function pyramid_volume with parameters base_length, base_width, and pyramid_height, that returns the volume of a pyramid with a rectangular base.

Relevant geometry equations:
Volume = base area x height x 1/3
Base area = base length x base width.
(Watch out for integer division). 
 
Code:
def pyramid_volume(base_length,base_width,pyramid_height):
    base_area=base_length*base_width
    pyramid_volume=base_area*pyramid_height*1/3
    return pyramid_volume
 
print('Volume for 4.5, 2.1, 3.0 is:', pyramid_volume(4.5, 2.1, 3.0))