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 =' ')
num = int(input("Enter a number: "))
ans=1
for i in range(1,num+1,1):
ans*=i
print("factorial "+str(num)+"!="+str(ans))
-----------------
fullname = input("Enter your full name:")
firstname =""
for c in fullname:
if c!=" " :
firstname+=c
else:
break
print("Have a great day, "+firstname+"!")
-----------------
for j in range (1,4):
for i in range(1,11):
print(i**j , end=' ')
print("\n")
----------------------
numStr = input("number")
num = int(numStr)
factorial = 1
for i in range(1, num+1):
factorial = factorial*i
print(factorial)
name1 = ""
for i in input ("what is your full name? "):
name1 = name1 + i
if i == " ":
print("Have a great day, "+name1+"!")
Problem 1:
numStr = input("give me a number ")
num = int(numStr)
factorial = 1
for i in range(1, num+1):
factorial = factorial*i
print(factorial)
Problem 2:
firstName = ""
for i in input ("what is your full name? "):
firstName = firstName + i
if i == " ":
print("Have a great day, "+firstName+"!")
Problem 3:
for i in range(1, 4):
for j in range(1, 11):
print(str(j**i) + " ", end = "")
print('\n')