Assessment#1
My biggest issue was creating a loop to cycle my program once executed so my users can restart within, rather than closing and re-launching it. I tested my loop() code which works on its own, works with selecting the count down, but e
ors when running the factorial. Any insight?
print(" XXXXXXXXXXLoops & Functions---------------")
def countdown():
count_down = intege
while (count_down >= 0):
XXXXXXXXXXprint(count_down)
XXXXXXXXXXcount_down -= 1
def factorial():
global intege
factorial = 1
if integer < 0:
XXXXXXXXXXprint("Factorial will not define for a negative number")
elif(integer == 0):
XXXXXXXXXXprint("The factorial of 0 is 1")
else:
XXXXXXXXXXwhile(integer > 0):
XXXXXXXXXXfactorial = factorial * intege
XXXXXXXXXXinteger -= 1
print("factorial of the given number is: ")
print("-----------------------------------------------")
print(factorial)
integer = int(input("Please enter a number greater than 1: ")) #Enter your integere here with 1 or greate
print()
print('User selected:', integer)
print()
print('Please select the following option below:')
print ()
print('Option 1: Your integer will count down from', integer)
print ()
print('Option 2: Find the factorial of', integer)
print()
if integer < 0:
print("please enter number greater than zero")
elif(integer == 0):
print("Please enter number greater than 0")
else:
select = int(input("Please select an option: ")) #Select either options 1 or 2 by inputing numbers 1-2
if select == 1:
XXXXXXXXXXcountdown()
elif select == 2:
XXXXXXXXXXfactorial()
print("-----------------------------------------------")
print("Thank you for testing my Program! Good day!")
Loop Code:
def loop():
repeat = input("Would you like to try again (Yes/No)? ").lower()
if repeat == "yes":
loop()
else:
print("Goodbye!")
exit()
loop()
I started my process by defining the countdown and factorial functions, utilizing the while loop, if-else, and elif statements to compute and execute the countdown from whichever integer is inputted along with its factorial. I find it quite frustrating how particular Python is with spacing and that every variable is case-sensitive.
Assessment#2
I was able to set the definitions of counting down as well as the factorials. After trial and e
or, I started the program off with defining the two items. I them set two user inputs (one for the number, and the other for the option). After the user inputs I put if options to determine how the inputs acted within the program. The only option I was struggling with and could not come up with is the negative number ending the program. I tried to understand this part and failed. If anyone has any suggestions or recommends another material I greatly appreciate. I look forward to hearing back from anyone.
#determine number to ente
x = ()
factorial = 1
#request number from end use
x = int(input("Please enter an integer (number) greater than 1:"))
#ask user which step to follow
selection = input ("Please enter 1 for a countdown or 2 to find the factorial:")
#print end user selection
if selection == "1":
while x > 0:
print (x)
x = x - 1
elif selection == "2":
# check if the number is negative, positive or zero
if x < 0:
print ("Factorial does not exist for negative numbers")
elif x == 0:
print ("The factorial of 0 is 1")
else:
for i in range(1,x + 1):
factorial = factorial*i
print ("The factorial of",x,"is",factorial)
else:
print ("Invalid Selection Entered")