Professional Documents
Culture Documents
All (Questions)
All (Questions)
Q2: Given the following grading scale, you are asked to write a program that will allow a
student to enter a test score and then display the proper message for that score.
#i entered 75
Q3: Write a python program that read number then find if this number odd or even.
if x % 2 == 0:
print("Even")
else:
print("Odd")
# 5
Odd
Q4: Write a python program that read two numbers and find if second is multiple of first.
if y % x == 0:
print("True")
else:
print("False")
#2 , 4
True
In [ ]: #import math
# ax2 + bx + c = 0
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
x = b**2 - 4*a*c
if x < 0:
print("The roots are complex numbers.")
else:
sq1 = (-b + (x)**(1/2)) / (2 * a)
sq2 = (-b - (x)**(1/2)) / (2 * a)
print("Root1:", sq1)
print("Root2:", sq2)
# 6 , -17 , 12
Root1: 1.5
Root2: 1.3333333333333333
else:
print("error")
# 7
7 is prime
Q7: Using a while loop, ask the user how long their bus ride was this month until they enter
0. Print the sum of these numbers.
In [ ]: sum = 0
data = int(input("How long your bus ride? (enter 0 to end): "))
while data != 0:
sum += data
data = int(input("How long your bus ride? (enter 0 to end): "))
print("Sum:", sum)
Sum: 600
Q8: Using a while loop, ask the user for the length of their bus/car ride until the user enters
0. Print the average of these numbers.
In [ ]: sum = 0
counter = 0
data = int(input("How long your bus ride? (enter 0 to end): "))
while data != 0:
sum += data
counter += 1
data = int(input("How long your bus ride? (enter 0 to end): "))
print("Average:", average)
#20 , 40 , 60 , 0 to end
Average: 40.0
Q9: Request the user to type numbers, each time printing its triple, until the user enters -1.
while x != -1:
print(3 * x)
x = int(input("Enter number: "))
# 3 , 6 , 5 , -1 to end
9
18
15
Q10: Write a Program to find out no. of Even & Odd no. in each Data Series and terminate if
entered value is -1
print("Odd:",odd)
print("Even:",even)
#1,2,3,4,5,6,7,8,9 , -1 to end
Odd: 5
Even: 4
Q11: Write a program that calculates and prints the product of the odd integers from 1 to
100.
In [ ]: product = 1
for i in range(1,101,2):
product *= i
print(product)
2725392139750729502980713245400918633290796330545803413734328823443106201171875
In [ ]: sum=0
count=0
List=[["Ali",50,10,80],["Noha",100,50,30],["Fadi",10,70,10]]
for i in range(len(List)):
#for j in range(len(List[i])):
sum+=List[i][1]
count+=1
print(sum/count)
List=[["Ali",50,10,80],
["Noha",100,50,30],
["Fadi",10,70,10]
]
for i in range(len(List)):
sum=0
count=0
for j in range(1,len(List[i])):
sum+=List[i][j]
count+=1
print(List[i][0],(sum/count))
53.333333333333336
Ali 46.666666666666664
Noha 60.0
Fadi 30.0
FILE 2
Q1: Write a python function that calculate the age by taking the year of birth
In [ ]: def year(x):
age = 2023 - x
#print("age is ",age)
return age
x = int(input("enter the year of birth"))
print("age is ",year(x))
age is 20
Q2: Write a program that read data from user then store in files.
my_file=open("the grades.txt","w")
x=int(input("enter num of std"))
for i in range(x):
id=str(input("enter id"))
std=str(input("enter name"))
grade=str(input("enter grade"))
my_file.write(id +" "+std +" "+ grade +"\n")
my_file.close()
my_file=open("the grades.txt","r")
print(my_file.read())
my_file.close()
1 abdo 3.8
2 bassel 3.0
Q3: Write a function divide () that used try command to prevent division of zero
In [ ]: try:
x=int(input("enter the "))
y=int(input("enter the "))
print(x/y)
except ZeroDivisionError :
print("your divide by 0")
# 10 , 0
your divide by 0
Q4: Assume a file containing a series of integers is named grades.txt and exists on the
computer’s disk. Write a program that calculates the average of all the numbers stored in the
file
In [ ]: new_file=open("grade.txt","w")
x=int(input("enter num of grades : "))
for i in range(x):
grade=str(input("enter the grade : "))
new_file.write(grade+"\n")
new_file.close()
new_file=open("grade.txt","r")
sum=0
count=0
for i in new_file:
sum+=int(i)
count+=1
print(sum/count)
new_file.close()
# 3 , 40 , 60 , 80
60.0
Q5: Write a Python program to read a file line by line and store it into a list.
In [ ]: new_file=open("grade.txt","r")
print(new_file.readlines())
new_file.close()
# we used the code above
In [ ]: import random
class coin :
def __init__(self,sideup="Head"):
self.sideup=sideup
def toss(self):
if random.randint(0,1)==0:
self.sideup="Tail"
else:
self.sideup="Head"
def get_sideup(self):
return self.sideup
coin1=coin()
coin1.toss()
print(coin1.get_sideup())
Head
def main():
window = tk.Tk()
window.title("Miles to kilometers Converter")
window.geometry("375x200")
label1.place(x=50, y=30)
label2.place(x=50, y =100)
In [ ]: import tkinter as tk
def convert():
miles = float(miles_entry.get())
kilometers = miles * 1.60934
result_label.config(text=f"{miles} miles is equal to {kilometers:.2f} kilometer