Created
August 1, 2021 16:35
-
-
Save shinysu/6955fd80ec61add885ab058f0906cd69 to your computer and use it in GitHub Desktop.
Day 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
using while loop to print numbers 1 to 10 | |
''' | |
i = 1 | |
while i <= 10: | |
print(i) | |
i = i+1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
sum of first n numbers using while loop | |
''' | |
n = int(input("Enter the range n: ")) | |
i = 1 | |
total = 0 | |
while i <= n: | |
total = total+i | |
i = i+1 | |
print(total) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
countdown from 10 to 0 and while 0, print 'Blast off' | |
''' | |
n = 10 | |
while n > 0: | |
print(n) | |
n = n - 1 | |
print("Blastoff!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Take input from the user and echo it until the user types 'done' | |
''' | |
while True: | |
line = input() | |
if line == 'done': | |
break | |
else: | |
print(line) | |
print('Thank you') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
The built-in function eval takes a string and evaluates it using the Python inter- preter. For example: | |
eval('1 + 2 * 3') | |
7 | |
Write a function called eval_loop that iteratively prompts the user, takes the resulting input and evaluates it using eval, and prints the result. | |
It should continue until the user enters 'done', and then return the value of the last expression it evaluated. | |
''' | |
while True: | |
exp = input("Enter the input expression: ") | |
if exp == 'done': | |
break | |
else: | |
result = eval(exp) | |
print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
* | |
* * | |
* * * | |
* * * * | |
* * * * * | |
- using for loop | |
''' | |
def row_line(n): | |
for i in range(n): | |
print('*', end=' ') | |
def pattern(rows): | |
for i in range(1, rows+1): | |
row_line(i) | |
print() | |
rows = int(input("Enter the number of rows: ")) | |
pattern(rows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
* | |
* * | |
* * * | |
* * * * | |
* * * * * | |
- using while loop | |
''' | |
def row_line(n): | |
i = 0 | |
while i < n: | |
i = i+1 | |
print('*', end=' ') | |
def pattern(rows): | |
i = 1 | |
while i < rows+1: | |
row_line(i) | |
i = i+1 | |
print() | |
rows = int(input("Enter the number of rows: ")) | |
pattern(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment