Any advice or improvements on my factorial calc? /u/WillingnessPast5294 Python Education

I’m currently relearning python and want to know if you have any advice or improvements you may add here. I’d like to learn more and understand more of python code.

#Factorial Calculator def factorial(): """Calculates the factorial of a number""" print('Factorial Calculator 1.0') while True: player_input = input('nWhat do number would you like to factorial?n>>>') if player_input.lower() == 'quit': print('Goodbye!') break elif player_input in ["*", "/", "+", "-"]: #Calls different function when the user inputs an operator factorial_operations(player_input) elif player_input.isnumeric(): #Checks if input is a number factorial = int(player_input) #Converts input to an integer conversion = [] #Empty list for the numbers factorial for num in range(1, factorial+1): conversion.append(str(num)) #Adds the range of the number in the conversion list multiplied = '*'.join(conversion) #Converts the list into a singular string joining them with * try: #Checks if an error occurs value = eval(multiplied) #Evaluates print('Calculating...') except RecursionError: #Goes back to the loop if it does error print('nNumber is too large!') else: print(f'nThe answer is: {value:,}') else: print('That is not a number.') def factorial_operations(player_input): """Calculates the division, subtraction, multiplication, and addition of factorials""" while True: first_input = input("What would be the first number?n>>>") second_input = input("The second number?n>>>") if first_input == 'quit' or second_input == 'quit': #Checks if any of the input is the trigger word print('Going back...') break elif first_input.isnumeric() and second_input.isnumeric(): first_input = int(first_input) second_input = int(second_input) first_conversion = [] second_conversion = [] for num in range(1, first_input+1): first_conversion.append(str(num)) for num in range(1, second_input+1): second_conversion.append(str(num)) first_multiplied = '*'.join(first_conversion) second_multiplied = '*'.join(second_conversion) first_value = eval(first_multiplied) second_value = eval(second_multiplied) match player_input: #Checks what the player's specified operation case '/': total = first_value // second_value case '*': total = first_value * second_value case '+': total = first_value + second_value case '-': total = first_value - second_value print(f'The answer is: {total:,}') break else: print('Cannot operate.') factorial() 

submitted by /u/WillingnessPast5294
[link] [comments]

​r/learnpython I’m currently relearning python and want to know if you have any advice or improvements you may add here. I’d like to learn more and understand more of python code. #Factorial Calculator def factorial(): “””Calculates the factorial of a number””” print(‘Factorial Calculator 1.0’) while True: player_input = input(‘nWhat do number would you like to factorial?n>>>’) if player_input.lower() == ‘quit’: print(‘Goodbye!’) break elif player_input in [“*”, “/”, “+”, “-“]: #Calls different function when the user inputs an operator factorial_operations(player_input) elif player_input.isnumeric(): #Checks if input is a number factorial = int(player_input) #Converts input to an integer conversion = [] #Empty list for the numbers factorial for num in range(1, factorial+1): conversion.append(str(num)) #Adds the range of the number in the conversion list multiplied = ‘*’.join(conversion) #Converts the list into a singular string joining them with * try: #Checks if an error occurs value = eval(multiplied) #Evaluates print(‘Calculating…’) except RecursionError: #Goes back to the loop if it does error print(‘nNumber is too large!’) else: print(f’nThe answer is: {value:,}’) else: print(‘That is not a number.’) def factorial_operations(player_input): “””Calculates the division, subtraction, multiplication, and addition of factorials””” while True: first_input = input(“What would be the first number?n>>>”) second_input = input(“The second number?n>>>”) if first_input == ‘quit’ or second_input == ‘quit’: #Checks if any of the input is the trigger word print(‘Going back…’) break elif first_input.isnumeric() and second_input.isnumeric(): first_input = int(first_input) second_input = int(second_input) first_conversion = [] second_conversion = [] for num in range(1, first_input+1): first_conversion.append(str(num)) for num in range(1, second_input+1): second_conversion.append(str(num)) first_multiplied = ‘*’.join(first_conversion) second_multiplied = ‘*’.join(second_conversion) first_value = eval(first_multiplied) second_value = eval(second_multiplied) match player_input: #Checks what the player’s specified operation case ‘/’: total = first_value // second_value case ‘*’: total = first_value * second_value case ‘+’: total = first_value + second_value case ‘-‘: total = first_value – second_value print(f’The answer is: {total:,}’) break else: print(‘Cannot operate.’) factorial() submitted by /u/WillingnessPast5294 [link] [comments] 

I’m currently relearning python and want to know if you have any advice or improvements you may add here. I’d like to learn more and understand more of python code.

#Factorial Calculator def factorial(): """Calculates the factorial of a number""" print('Factorial Calculator 1.0') while True: player_input = input('nWhat do number would you like to factorial?n>>>') if player_input.lower() == 'quit': print('Goodbye!') break elif player_input in ["*", "/", "+", "-"]: #Calls different function when the user inputs an operator factorial_operations(player_input) elif player_input.isnumeric(): #Checks if input is a number factorial = int(player_input) #Converts input to an integer conversion = [] #Empty list for the numbers factorial for num in range(1, factorial+1): conversion.append(str(num)) #Adds the range of the number in the conversion list multiplied = '*'.join(conversion) #Converts the list into a singular string joining them with * try: #Checks if an error occurs value = eval(multiplied) #Evaluates print('Calculating...') except RecursionError: #Goes back to the loop if it does error print('nNumber is too large!') else: print(f'nThe answer is: {value:,}') else: print('That is not a number.') def factorial_operations(player_input): """Calculates the division, subtraction, multiplication, and addition of factorials""" while True: first_input = input("What would be the first number?n>>>") second_input = input("The second number?n>>>") if first_input == 'quit' or second_input == 'quit': #Checks if any of the input is the trigger word print('Going back...') break elif first_input.isnumeric() and second_input.isnumeric(): first_input = int(first_input) second_input = int(second_input) first_conversion = [] second_conversion = [] for num in range(1, first_input+1): first_conversion.append(str(num)) for num in range(1, second_input+1): second_conversion.append(str(num)) first_multiplied = '*'.join(first_conversion) second_multiplied = '*'.join(second_conversion) first_value = eval(first_multiplied) second_value = eval(second_multiplied) match player_input: #Checks what the player's specified operation case '/': total = first_value // second_value case '*': total = first_value * second_value case '+': total = first_value + second_value case '-': total = first_value - second_value print(f'The answer is: {total:,}') break else: print('Cannot operate.') factorial() 

submitted by /u/WillingnessPast5294
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *