Loops are foundational for computer programming. Almost every script uses at least one loop.
Do not use function calls instead of looping. Make sure the your scripts have a while loop or a for loop.
Natural numbers are whole numbers starting at 1. So, 1, 2, 3, 4, 5, 6, ...
Write a Python program that asks for a number from the user and then displays the sum of all of the natural numbers up to and including that number. Use a while loop.
If user input is 5, the output is 15 (1+2+3+4+5 = 15).
Example 1:
Please enter a number: 3 The sum of the natural numbers up to and including 3 is 6
Example 2:
Please enter a number: 40 The sum of the natural numbers up to and including 40 is 820Provided code:
def main(): main()
Natural numbers are whole numbers starting at 1. So, 1, 2, 3, 4, 5, 6, ...
Complete the productOf() function to take a number an int as a parameter. Use a for loop and an accumulator to calculate the product of all of the natural numbers between 1 and the parameter's value. Return this product.
Example 1:
productOf( 4 ) returns 24 (because 1*2*3*4 = 24)
Example 2:
productOf( 10 ) returns 3628800 (because 1*2*3*4*5*6*7*8*9*10 = 3628800)
def productOf():
Write a Python program that requests the following three int values from the user:
Example 1:
Please enter a starting number: 10 Please enter an ending number: 22 Please enter an increment size: 3 10 13 16 19(Notice that 22 is not included in the output)
Example 2:
Please enter a starting number: 0 Please enter an ending number: 51 Please enter an increment size: 10 10 20 30 40 50
Example 3:
Please enter a starting number: 5 Please enter an ending number: 0 Please enter an increment size: -1 5 4 3 2 1(Notice that the increment size is negative) Provided code:
def main(): main()
Write a Python program that requests the following three int values from the user:
Example 1:
Please enter a starting number: 10 Please enter an ending number: 22 Please enter an increment size: 3 10, 13, 16, 19.
Example 2:
Please enter a starting number: 0 Please enter an ending number: 101 Please enter an increment size: 10 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.
Example 3:
Please enter a starting number: 10 Please enter an ending number: 0 Please enter an increment size: -1 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.
Example 4:
Please enter a starting number: 10 Please enter an ending number: 100 Please enter an increment size: -1 .(Notice that no number series was displayed, just the period)
Example 5:
Please enter a starting number: 1000 Please enter an ending number: 10 Please enter an increment size: 1 .(Notice that no number series was displayed, just the period)
Hints:
def main(): main()
Complete the getLastValue() function to take the following three int values:
Example 1:
getLastValue( 10, 22, 3 ) returns 19
Example 2:
getLastValue( 0, 101, 10 ) returns 100
Example 3:
getLastValue( 10, 0, -1 ) returns 1
Example 4:
getLastValue( 10, 100, -1 ) returns None
Example 5:
getLastValue( 1000, 10, 1 ) returns None
Hints:
def getLastValue( ): # Examples 1 - 5 print( getLastValue( 10, 22, 3 ) ) print( getLastValue( 0, 101, 10 ) ) print( getLastValue( 10, 0, -1 ) ) print( getLastValue( 10, 100, -1 ) ) print( getLastValue( 1000, 10, 1 ) )
In general, after a payment is due for a credit card, the balance increases daily due to interest. The interest amount is calculated by multiplying the balance by the daily interest rate. The interest is then added to the balance. The daily interest rate is the annual percentage rate (APR) divided by 365. For example, if you have a $1000.00 balance on your credit card and you have a "store credit card" with the average APR of 25.74% (Source: https://wallethub.com/edu/cc/average-credit-card-interest-rate/50841/), then your daily interest rate is about 0.071% (= 25.74/365) and the balance after one day is $1000.71 (= 1000*0.071/100). (Notice that we use the rate form (0.071/100) instead of the percentage form of 0.071% to perform the calculations.) The balance on the next day is about $1001.41 (= $1000.71*0.071/100). This continues until the balance is paid in full.
Complete the futureBalance() function to have the following parameters:
Example 1:
futureBalance( 1000.00, 25.74, 1 ) returns (about) 1000.71 (see description above for details)
Example 2:
futureBalance( 1000.00, 25.74, 2 ) returns (about) 1001.41 (see description above for details)
Example 3:
futureBalance( 1000.00, 25.74, 180 ) returns (about) 1135.29
Example 4:
futureBalance( 1000.00, 25.74, 365 ) returns (about) 1293.45
(Wow, that's what happens if you owe $1000 and don't pay it off for one year, you now owe $1293.45!)
def futureBalance( ): # Examples 1 - 4 print( futureBalance( 1000.00, 25.74, 1 ) ) print( futureBalance( 1000.00, 25.74, 2 ) ) print( futureBalance( 1000.00, 25.74, 180 ) ) print( futureBalance( 1000.00, 25.74, 365 ) )
Write a Python program that does the following:
Example 1:
Welcome to my inches to feet converter! Please enter a number of inches: 32 2.7 feet Please enter a number of inches: 60 5.0 feet Please enter a number of inches: -20 Please enter a positive number! Please enter a number of inches: 40 3.3 feet Please enter a number of inches: 0 Have a nice day!
Example 2:
Welcome to my inches to feet converter! Please enter a number of inches: -32 Please enter a positive number! Please enter a number of inches: -21 Please enter a positive number! Please enter a number of inches: -10 Please enter a positive number! Please enter a number of inches: 0 Have a nice day!
def main(): main()
Write a Python program that will ask a user to enter a number. Display the number and whether it is odd or even. After that your program should ask if user wants to continue. If the user enters 'y', then repeat the steps above. If user chooses anything else, display a goodbye message. You can assume that the numbers that the user will enter are integers.
Note, feel free to use your isOdd() function from your previous practice assignment.
Example 1:
Please enter a number: 7 7 is an odd number Do you want to continue (y or n): y Please enter a number: 24 24 is an even number Do you want to continue (y or n): n Have a great day!
Example 2:
Please enter a number: 1 1 is an odd number Do you want to continue (y or n): n Have a great day!
def main(): main()
Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not.
Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test:
Examples:
isPrime( 2 ) returns True
isPrime( 3 ) returns True
isPrime( 4 ) returns False (4 is divisible by 2)
isPrime( 7 ) returns True
isPrime( 9 ) returns False (9 is evenly divisible by 3)
isPrime( 101 ) returns True
Hint:
In the note above, do you see a pattern that you can use a loop to execute?
def isPrime(num):
Write a Python program that prompts the user for a integer and then displays all of the prime numbers between 2 and the user's input (including what the user input) (with each number on it's own line).
Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2).
Example 1:
Please enter a number: 10 2 3 5 7
Example 2:
Please enter a number: 23 2 3 5 7 11 13 17 19 23
Write a calculator that will give the user the following menu options:
1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit
Example 1:
Let's calculate! 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Please select one of options above: 4 Enter the first number: 2.81 Enter the second number: 1.111 Answer: 2.5 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Please select one of options above: 5 Have a good day!
def main(): main()