# -*- coding: utf-8 -*- """ Created on Wed Sep 30 11:47:45 2020 @author: jbobowsk """ # This tutorial will introduce simple implementations of while loops # in Python. # Here's a while loop that calculates the factorial of n. Start with n = 4 # to make sure things are working properly. The loop executes until n > 1. # That is, the the last iteration of the loop will occur when n = 2. (You # could actually let the while loop run until n = 1 since factorial*1 = # factorial). n = 4 print('n =', n) factorial = 1 while n > 1: factorial = factorial*n n = n - 1 print('n! =', factorial) # Here's a more interesting factorial n = 50 print('n =', n) factorial = 1 while n > 1: factorial = factorial*n n = n - 1 print('n! =', factorial) # We can force the number to be written in scientific notation while keeping # only have digits after the decimal point using: print('n! =', float(factorial)) # Here's a a crazy big number... n = 170 print('n =', n) factorial = 1 while n > 1: factorial = factorial*n n = n - 1 print('n! =', float(factorial)) # Here's the factorial of a user-entered value. n = int(input("Enter an integer: ")) print('n =', n) factorial = 1 while n > 1: factorial = factorial*n n = n - 1 print('n! =', float(factorial)) # Maybe you want to the user to be able to enter only integer numbers with # appropriate error messages. # See the nested control structure tutorial for an implementation of this # check. That tutorial will also show you how to calculate the factorials # of a bunch of n values and then plot the results vs n.