# # Python programming # # Unit 1 - Basic components of a Python program # # Guided practice # # Preliminary remarks # - Required knowledge # - Avoid using more advanced material # - Try yourself before watching # Q. 1 # Write a program that recieves four digits and prints # them in the shape of a rhombus. E.g. # 4 # 3 2 # 1 digit1 = input("Please enter a digit: ") digit2 = input("Please enter a digit: ") digit3 = input("Please enter a digit: ") digit4 = input("Please enter a digit: ") print("", digit1, "") print(digit2, digit3) print("", digit4, "") # Q. 2 # Write a program that receives four positive integers # and prints them rotated 1 position to the left. E.g. # if the program receives 7, 8, 9, 6, in this order, # and prints: # 8 9 6 7 int1 = input("Please enter a positive integer: ") int2 = input("Please enter a positive integer: ") int3 = input("Please enter a positive integer: ") int4 = input("Please enter a positive integer: ") print(int2, int3, int4, int1) # Q. 3 # Write a program that receives four floats, and then # Prints the fourth, the second, the third and finally # the first. Indicate the printing order # using proper messages. float1 = input("Please enter a decimal number: ") float2 = input("Please enter a decimal number: ") float3 = input("Please enter a decimal number: ") float4 = input("Please enter a decimal number: ") print("This is the forth number:", float4) print("This is the second number:", float2) print("This is the third number:", float3) print("This is the first number:", float1) # Q. 4 # Write a program that receives three floats, # divides each number by 5, and prints the sum of # the division results, accompanied by a proper message. float1 = float(input("Please enter a decimal number: ")) float2 = float(input("Please enter a decimal number: ")) float3 = float(input("Please enter a decimal number: ")) print("Sum of divisions: ") print(float1 / 5 + float2 / 5 + float3 / 5) # Q. 5 # Write a program that receives the side of a square # and prints the area of the square and its perimeter. side = float(input("Please enter a side of a square: ")) print("The area: ", side ** 2) print("The perimeter: ", 4 * side) # Q. 6 # Assumming that there are exactly 365 days in a year, # write a program that receives a number of days # and calculates the number of years in all these days. days = int(input("Please enter number of days: ")) print(days // 365) # Q. 7 # Phileas Fogg committed himself to travelling the # entire world in 80 days. Embarking on his mission # some days ago, he is currently somewhere on our planet. # Write a program that receives the number of days # passed since he departed London, then calculates # the number of days left to the end of his journey, # and finally prints the result of this calculation. days = int(input("Please enter number of days: ")) print("Days left: ", 80 - days) # Q. 8 # Write a program that recieves the salary of an # employee (float), and also an amount of bonus, # in precents. The program calculates and prints # the new salary. salary = float(input("Please enter salary: ")) bonus = float(input("Please enter bonus (0-100): ")) print(salary + salary * (bonus / 100)) # Q. 9 # Roni, Nomi, and the bear Jacob, each toss a certain # dice twice. # The numbers 1-6 appear on the six sides of the dice # The dice has 6 sides. # Write a program that simulates the game and prints # the sum of numbers appearing on the upper face of # the three dices in the first and second round of the game. import random roni1 = random.randint(1, 6) nomi1 = random.randint(1, 6) yaakov1 = random.randint(1, 6) print(roni1 + nomi1 + yaakov1) roni2 = random.randint(1, 6) nomi2 = random.randint(1, 6) yaakov2 = random.randint(1, 6) print(roni2 + nomi2 + yaakov2) # Q. 10 # Write a program that receives two digits of # a positive integer in range 10-99, the first # is the ones digit, the second is the tens digit. # The program prints the number. ones = int(input("Please enter the ones digit: ")) tens = int(input("Please enter the tens digit: ")) print(tens * 10 + ones) # Q.11 # Write a program that receives three digits of # a positive integer in range 100-999, the first # is the ones digit, the second is the tens digit, # the third is the hundreds digit. # The program prints the number. ones = int(input("Please enter the ones digit: ")) tens = int(input("Please enter the tens digit: ")) hundreds = int(input("Please enter the hundreds digit: ")) print(hundreds * 100 + tens * 10 + ones) # Q. 12 # Write a program that receives a positive integer # in range 10-99 and prints the sum of its digits. num = int(input("Please enter a number (10-99): ")) ones = num % 10 tens = num // 10 print(ones + tens) # Q. 13 # Write a program that receives a positive integer # in range 100-999 and prints the sum of its digits. # Challenge: do the same for a four-digit integer. num = int(input("Please enter a number (100-999): ")) ones = num % 10 tens = (num % 100) // 10 hundreds = num // 100 print(ones + tens + hundreds) # Q. 14 # Write a program that receives a number # representing a certain weekday (1-7), and also a # number of days which passed from that day till today. # The program prints the number representing today. day = int(input("Please enter weekday (1-7): ")) days = int(input("Please enter number of days passed: ")) print((day + days) - ((day + days) // 7) * 7) 1 2 3 4 5 6 7 1 2 3 4 3 + 8 = 11 3 + 20 -> 23, 23 - 21 = 2 print(23 - (23 // 7) * 7 ) 23 = 3 * 7 + 2