Homework:
Using nested for loops, print a triangle with this pattern:
*
* * * *
* * * * * * * * *
* * * * * * * * * * * * * * * *
Create a list. Using a while loop, keep taking user input, and add the input to a list until the user enters “STOP”. Then, print all the elements of the list backwards, spaced with a tab.
Iterate through a loop of positive and negative integers. If the element is positive, append it to a new list. Print the new list.
Use either for i in listName or for i in range(len(listName))
Resources:
Class slides: https://docs.google.com/presentation/d/1sOa_dL0L5SY4MS3t7HqVnIOU-PW4F9Ze_ZOgh0ySVmQ/edit?usp=sharing
Code: https://replit.com/@ShravyaS/IntroToPython-Wk3Day1#main.py
To view the code, open the link, and on the top left of the box, there is a button to show files. Click that and then click on main.py. If I got something wrong or it doesn't work send me a screenshot and I'll figure it out.
Coding platform: replit.com
Post your answers as a comment! I will post the solution before next class.
You can always email us at shravyassathish@gmail.com (mentor) or kingericwu@gmail.com (TA) or message us on Discord if you have any questions about the homework or what we covered in class.
for i in range(1,5):
for j in range(1,i**2 + 1):
print("* ", end = "")
print("\n")
list1 = []
while True:
input1 = input("enter a string: ")
if input1 == 'stop':
break
list1.append(input1)
list1.reverse()
print('\t'.join(list1))
list2 = [1,2,3,-4,5,6,-7,8,-9,-5]
list3 = []
for i in list2:
if i > 0:
list3.append(i)
for j in list3:
print(j, end = '')
for i in range(1,5):
for j in range (i):
print("*", end=' ')
print("\n")
inputStr=print("Enter STOP to stop")
inputStr=input("Please enter a string: ")
list=[];
while inputStr.upper()!="STOP" :
list.append(inputStr)
inputStr=input("Please enter a string: ")
length= len(list)
for i in range(length):
if(i== length-1):
print(list[length-i-1])
else:
print(list[length-i-1], end='\t')
print("\n")
problem 1: j=1
for i in range(4):
#print("i="+str(i))
#print("j="+str(j))
for j in range(j):
print("*", end=' ')
j+=4+i*2
print("\n")
problem2: inputStr=print("Enter STOP if you want to stop!")
inputStr=input("Please enter a string: ")
list=[];
while inputStr.upper()!="STOP" :
list.append(inputStr)
inputStr=input("Please enter a string: ")
size= len(list)
for i in range(size):
if(i== size-1):
print(list[size-i-1])
else:
print(list[size-i-1], end='\t')
print("\n") problem3: list=[1,-10,3,5,-99,-57,35,221,4]
list2=[]
for i in list:
if i>=0:
list2.append(i)
for j in list2:
print(j, end=' ')