Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created August 1, 2021 16:35
Show Gist options
  • Save shinysu/6955fd80ec61add885ab058f0906cd69 to your computer and use it in GitHub Desktop.
Save shinysu/6955fd80ec61add885ab058f0906cd69 to your computer and use it in GitHub Desktop.
Day 6
'''
using while loop to print numbers 1 to 10
'''
i = 1
while i <= 10:
print(i)
i = i+1
'''
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)
'''
countdown from 10 to 0 and while 0, print 'Blast off'
'''
n = 10
while n > 0:
print(n)
n = n - 1
print("Blastoff!")
'''
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')
'''
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)
'''
*
* *
* * *
* * * *
* * * * *
- 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)
'''
*
* *
* * *
* * * *
* * * * *
- 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