Introduction to Python Functions Create a function named com

Introduction to Python: Functions

Create a function named compute_balance which takes two floating point parameters. Pass in the current_balance and the payment as floating point values. Set a default value for the current_balance to 1000 and the default value for the payment to 20. Subtract the payment from the current_balance. If the current_balance is greater than 0, return a string which has \"balance owed\" and return current_balance. If the current_balance is less than 0, return \"credit\" and return current_balance as a credit (make this value positive). If the current_balance after the payment is 0, return \"paid in full\" and 0. Call the function from within main() and pass in 500 for the current_balance. Print out the two returned values. Call the function from within main() and pass in 1100 for the payment. Print out the two returned values. Call the function from within main() and pass 1000 for the payment. Print out the two returned values.

Then, Create a function named find_sum which will take in two integer parameters lower_param and upper_param. Compute the sum of all of the numbers from lower_param to upper_param. Return the sum. Call the function from within main() and pass in 5 and 10. Print out the returned sum.

Finally, Create a function named roll_dice that will call the randint function twice to generate two random numbers between 1 and 6. Calculate the sum of the two random numbers. If the sum is 7, print \"You win!\" and return the sum. Otherwise, print \"Roll again\" and the return the sum. Call the function roll_dice from the main() function. Write a loop which will continue until the sum is 7.

Solution

1.

def compute_balance(current_balance = 1000, payment = 20):
   current_balance = current_balance - payment
   if current_balance > 0:
       print \'balance owed\'
       return current_balance
   elif current_balance < 0:
       print \'credit\'
       return current_balance
   else:
       print \'paid in full\'
       return current_balance


def main():
   compute_balance(500)
   compute_balance(payment = 1100)
   compute_balance(payment = 1000)

2.

def find_sum(lower_param, upper_param):
   sum = 0
   for i in range(lower_param, upper_param + 1):
       sum = sum + i
   return sum


def main():
   print find_sum(5, 10)
  

3.

import random

def roll_dice():
   a = randint(1, 6)
   b = randint(1, 6)
   total = a + b
   if total == 7:
       print \'You win!\'
   else:
       print \'Roll again\'

def main():
   total = roll_dice()
   while total != 7:
       total = roll_dice()

Introduction to Python: Functions Create a function named compute_balance which takes two floating point parameters. Pass in the current_balance and the payment
Introduction to Python: Functions Create a function named compute_balance which takes two floating point parameters. Pass in the current_balance and the payment

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site