AP computer Science Functions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

6.4.8

def add(in1,in2): return(str(in1+in2)) print(add(1,2)) ## Be sure to re-indent the code!

6.3.7

def add(num1,num2): print(str(num1) + " + " + str(num2) + " = " + str(num1+num2)) def subtract(num1,num2): print(str(num1) + " - " + str(num2) + " = " + str(num1-num2)) def multiply(num1,num2): print(str(num1) + " (ASTERISK)" + str(num2) + " = " + str(num1(ASTERISK)num2)) num1 = int(input("What is the first number?:")) num2 = int(input("What is the second number?:")) op = input("Choose an operation (add, subtract, multiply):") if op == "add": add(num1,num2) elif op == "subtract": subtract(num1,num2) elif op == "multiply": multiply(num1,num2) ## Be sure to re-indent the code!

6.3.5

def add_nums(num1, num2): sum = num1 + num2 print(sum) add_nums(5, 6) ## Be sure to re-indent the code!

6.2.8

def calculate_area(side_length = 10): print("The area of a square with sides of length " + str(side_length) + " is " + str(side_length*side_length)+".") inpt = int(input("Enter side length:")) if inpt > 0: calculate_area(inpt) else: calculate_area() ## Be sure to re-indent the code!

6.4.9

def change_to_F(temp): temp = (1.8*temp) + 32 return temp # Now write your function for converting Fahrenheit to Celsius. def change_to_C(temp): temp = (temp - 32) / 1.8 return temp # Now change 0C to F: print(change_to_F(0)) # Change 100C to F: print(change_to_F(100)) # Change 40F to C: print(change_to_C(40)) # Change 80F to C: print(change_to_C(80)) ## Be sure to re-indent the code!

6.5.5

def change_to_F(temp): temp = (1.8*temp) + 32 return temp def change_to_C(temp): temp = (temp - 32) / 1.8 return temp temp = float(input("enter temp idot")) try: print(change_to_F(temp)) except (ValueError): print("Noe!") ## Be sure to re-indent the code!

6.2.6

def print_multiple_times(str,in): for x in range(in): print(str) print_multiple_times("hi",3) ## Be sure to re-indent the code!

6.1.5

def prnt_rain(): print("On a rainy day, galoshes are appropriate footwear.") def prnt_snowy(): print("If it is snowy, you should suggest I wear boots") def prnt_sunny(): print("If it is sunny, you should suggest I wear sandals") inpt = input("What is the weather? (sunny,rainy,snowy):") if inpt == "rainy": prnt_rain() elif inpt == "sunny": prnt_sunny() elif inpt== "snowy": prnt_snowy() ## Be sure to re-indent the code!

6.2.5

def product(int1,int2): print(str(int1*int2)) product(1,2) ## Be sure to re-indent the code!

6.5.6

def retrieve_positive_number(): while True: num = input("Enter numberrr") try: num = int(num) except ValueError: print("NOT VALIDDD!") continue if num % 2 != 0 or num < 1: print("not CORRECT!!!!1!") else: return num retrieve_positive_number() ## Be sure to re-indent the code!

6.3.6

num4 = 10 num3 = int(input("Enter a number")) def add(): print(str(num1+num2)) add() ## Be sure to re-indent the code!


Kaugnay na mga set ng pag-aaral

OB Module 2: Care of the Pregnant Woman

View Set

Chapter 13: The Spinal Cord, Spinal Nerves, and Somatic Reflexes

View Set

Sayles Ch 20 Quiz: Human Resource Management

View Set

Ch. 13 Narcotics: Opiates, Morphine, Heroin

View Set

Chapter One - Observation Skills (Forensic Science)

View Set

A&P II Section 1.3 Heart Conduction System

View Set

Vocab Unit 10: Synonyms and Antonyms

View Set

Sociology Chapter 8: Inquizative homework

View Set