HOMEWORK:
Take user input for a number and find its factorial (ex: 5! = 5 * 4 * 3 * 2 * 1 = 120) using a for loop.
Take user input for someone’s full name. Then create a separate variable for only their first name, and greet them in the format “Have a great day, {firstName}!”
One approach: For loop to locate space. First name = before space.
Use nested loops to print 3 rows: the first with all numbers from 1 to 10, the second with all their squares, and the third with all their cubes.
Use end=' ' parameter in inner loop and print(‘\n’) in your outer loop (under the inner loop) for correct spacing.
RESOURCES:
Class slides:
https://docs.google.com/presentation/d/1DpbCZWxsHFFkeYu6DJOHEDSKzi0I0DudeHR8OiBTSK0/edit?usp=sharing
Example code: https://replit.com/@ShravyaS/IntroToPython-Wk2Day2
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.
Solution:
Part 1:
x=int(input())
num=1
while x >0:
num=num*x
x=x-1
print(num)
Part 2:
name=input()
firstname=""
for char in name:
if char == " ":
break
firstname+=char
print(f'Have a great day, {firstname}!')
Part 3:
for i in range(1,11):
print(i, end=' ')
print()
for i in range(1,11):
print(i*i, end =' ')
print()
for i in range(1,11):
print(i*i*i, end =' ')