In: Computer Science
Python
age = input(‘How old are you? ’) if age >= 14
print(‘You may go on the rollercoaster’)
Read carefully - What will the program output if the user types ‘15’?
2. Concisely explain the difference between ‘while’ and ‘for’ ______________________________________________
3.The __________statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
4.The ____________ statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but executes the next iteration of the loop.
______________________________________________
Question 1.
Output:
No output. It will throw an error that " >= not supported between instances of str and int"
Explanation:
In the input we are taking input as a string. So 15 will be read as a string not integer. And in if statement we are comparing age which is string with 14 which is integer so there will be an error hence program will get terminated stating the above error.
To read a input as an integer.
age=int(input ("How old are you?")
Question 2:
While loop | for loop |
The while statement loops untill a condition is false. |
Iterate through a loop with definite range or collections Example: for i in range(5): |
While loop is used when the number of iteration are not exactly known | for loop is used when we exactly know the number of iterations. |
Question 3:
The BREAK statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
Question 4:
The CONTINUE statement is used to skip the rest of the code inside a loop for the current iteration only. Loop doesn't terminate but executeas the next iteration of the loop.