# # if, if...else, if...elif...else # # Practice # # Preliminary notes: - Presumed knowledge - Use only the studied material # Q. 1 # Write a program that receives a float and prints # POSITIVE if it is positive, NON-POSITIVE otherwise. num = float(input("Plase enter a decimal number: ")) if num > 0: print("Positive") else: print("Non-positive") print("THE END") # Q. 2 # Write a program that receives two strings, s1 and s2, # and prints EQUAL if they are equal, DIFFERENT otherwise. s1 = input("Plase enter any string: ") s2 = input("Plase enter any string: ") if s1 == s2: print("EQUAL") else: print("DIFFERENT") print("THE END") # Q. 3 # Noa plans to subscribe to the New York Times. # Monthly subscription costs 5 dollars. # 1 dollar reduction is given if # (subscriber-s age is under 20 # and # subscription is to more than 12 months) # or # (age is above 60 # and # subscription is to at least 6 months). # Write a program that receives Noa-s age # and number of subscription monthes, # and then prints the cost of her subscription. age = int(input("Please enter your age: ")) months = int(input("Please enter number of months: ")) monthlyPayment = 5 if (age < 20 and months > 12) or (age > 60 and months >= 6): monthlyPayment = 4 totalCost = months * monthlyPayment print(totalCost) # Q. 4 # Write a program that receives two integers, # num1 and num2, and prints YES if num2 divides # num1 without a reminder, NO otherwise. num1 = int(input("PLease enter an integer: ")) num2 = int(input("PLease enter an integer: ")) if num1 % num2 == 0: print("YES") else: print("NO") print("THE END") # Q. 5 # Write a program that receives two numbers, # num1 and num2, different one from another, # and prints the bigger number, then the smaller, # each in a seprate line. num1 = int(input("PLease enter an integer: ")) num2 = int(input("PLease enter a different integer: ")) if num1 > num2: print(num1) print(num2) else: print(num2) print(num1) print("THE END") # Q. 6 # One scoop of chocolate ice cream costs 5 NIS, # unless you buy more than 3 scoops. In that case # a scoop costs 2 NIS. # Write a program that receives a number of scoops # and prints their total cost. scoopPrice = 5 scoopsNum = int(input("Please enter number of scoops: ")) if scoopsNum > 3: scoopPrice = 2 print("Total cost of scoops is: ", scoopsNum * scoopPrice) # Q. 7 # In your friend-s school grades are marked # by the letters A, B, C, D, E and F. # Write a program that receives a letter and # prints OK if it is a grade mark, NO otherwise. s = input("Please enter any string: ") if (s == "A" or s == "B" or s == "C" or s == "D" or s == "E" or s == "F"): print("OK") else: print("NO") print("THE END") # Q. 8 # Write a program that receives two integers, # num1 and num2, and prints OK if one is consecutive # to the other, NO otherwise. num1 = int(input("Please enter an integer: ")) num2 = int(input("Please enter an integer: ")) if num1 + 1 == num2 or num2 + 1 == num1: print("OK") else: print("NO") print("THE END") # if num2 - num1 == 1 or num1 - num2 == 1: # Q. 9 # Write a programm that receives three intengers # representing a series, a1, a2 and a3. # The program will print DESCENDING if the # series is descending, NON DESCENDING otherwise. a1 = int(input("Please enter an integer: ")) a2 = int(input("Please enter an integer: ")) a3 = int(input("Please enter an integer: ")) if a1 > a2 and a2 > a3: print("descending") else: print("non-descending") print("THE END") 1, 2, 3 2, 4, 6 3, 6, 9 3, 2, 1 # Q. 10 # Write a program that receives a three-digit # integer. It prints PLAINDROME if the number # is a palindrome, NO otherwise. num = int(input("Please enter a three-digit integer: ")) if num % 10 == num // 100: print("PALINDROME") else: print("NO") print("THE END") 79797 1221 123 % 10 print(797 // 100) # Q. 11 # Write a program that receives a three digit number. # The program prints: # 1 - if the ones digit is 5 # 2 - if the tens digit is odd # 3 - if the hundreds digit divides by 3 with no reminder. # if neither 1, nor 2, nor 3 are printed, the program : # print PERHAPS NEXT TIME. num = int(input("Please enter a three-digit integer: ")) if num % 10 == 5: print(1) elif ((num % 100) // 10) % 2 == 1: print(2) elif (num // 100) % 3 == 0: print(3) else: print("PERHAPS NEXT TIME") print("THE END") # Q. 12 # Write a program that receives three numbers # and prints them, in one line, from the smallest # to the biggest. num1 = int(input(input("Please enter an integer: "))) num2 = int(input(input("Please enter an integer: "))) num3 = int(input(input("Please enter an integer: "))) 9 , 7, 3 if num1 < num2: if num2 < num3: print(num1, num2, num3) else: if num1 < num3: print(num1, num3, num2) else: print(num3, num1, num2) else: Q. 13 Write a program that receives two three-digit numbers, num1 and num2, and prints EQUAL if all digits of num1 appear in num2, UNEQUAL otherwise. num1 = int(input("Please enter a three-digit integer: ")) ones1 = num1 % 10 tens1 = (num1 % 100) // 10 hundreds1 = num1 // 100 print(ones1, tens1, hundreds1) num2 = int(input("Please enter a three-digit integer: ")) ones2 = num2 % 10 tens2 = (num2 % 100) // 10 hundreds2 = num2 // 100 print(ones2, tens2, hundreds2) result = "UNEQUAL" if ones1 == ones2 or ones1 == tens2 or ones1 == hundreds2: if tens1 == ones2 or tens1 == tens2 or tens1 == hundreds2: if hundreds1 == ones2 or hundreds1 == tens2 or hundreds1 == hundreds2: result = "EQUAL" print(result ) 583 335 if (5 == 5): if (3 == 5): if (5 == 5): print("SOMETHING") else: print("ERROR") else: print("ERROR") else: print("ERROR") print("THE END")