0% found this document useful (0 votes)
5 views9 pages

Py Ws 4

The document contains a collection of Python code snippets, each demonstrating various programming concepts such as functions, input/output, and basic arithmetic operations. Each snippet includes a main function that calls other functions to perform tasks like string manipulation, mathematical calculations, and user interaction. The examples cover a range of topics from basic input handling to more complex operations like calculating taxes and converting units.

Uploaded by

unknownguy3023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Py Ws 4

The document contains a collection of Python code snippets, each demonstrating various programming concepts such as functions, input/output, and basic arithmetic operations. Each snippet includes a main function that calls other functions to perform tasks like string manipulation, mathematical calculations, and user interaction. The examples cover a range of topics from basic input handling to more complex operations like calculating taxes and converting units.

Uploaded by

unknownguy3023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Problem sheet 4

1.def main():
x=input("Enter a string:")
upp(x)

def upp(st):
x=st.upper()
x=x+"!"
print(x)
main()

2.def show_value(quantity):
print(quantity)
def main():
x=12
show_value(x)
main()

3.A=3,B=2,C=1

4.def main():
x=1
y = 3.4
print(x, y)
change_us(x, y)
print(x, y)
def change_us(a, b):
a=0
b=0
print(a, b)
main()
5.def my_function(x, y):
return x[y]
def main():
x=(input("Enter x string:"))
y=int(input("Enter y:"))
z=my_function(x,y)
print(z)
main()

6.import random
rand=random.randint(0,100)
print(rand)

7.def half(x):
y=x//2
print(y)
def main():
x=float(input("Enter a number:"))
half(x)
main()

8.def cube(num):
x=num*num*num
print(x)
def main():
x=int(input("Enter a number:"))
cube(x)
main()

9.def times_ten(x):
return x*10
def main():
x=int(input("Enter a number:"))
print(times_ten(x))
main()

10.def is_valid_length(string, n):


if len(string)>n:
print("false")
else:
print("true")
def main():
s=input("Enter a string:")
x=int(input("Enter a number:"))
is_valid_length(s,x)
main()

11.def m_km(km):
print("In miles:",km*0.6421)
def main():
km=eval(input("Enter km:"))
m_km(km)
main()

12.def repeat(string, x):


print(string*x)
def main():
s=input("Enter a string:")
x=int(input("Enter a number:"))
repeat(s,x)
main()
13.def insur(x):
amt=x*(4/5)
print("The min amount of insurance required is:",amt)
def main():
x=int(input("Enter the replacement cost of the building:"))
insur(x)
main()

14.def auto(insur, loan, gas, oil, tires, maint):


tot=insur+loan+gas+oil+tires+maint
print("The total monthly expense is:",tot)
print("The total annual expense is:",tot*12)
def main():
insur=int(input("Enter expense in insurance:"))
loan=int(input("Enter expense in loan:"))
gas=int(input("Enter expense in gas:"))
oil=int(input("Enter expense in oil:"))
tires=int(input("Enter expense in tires:"))
maint=int(input("Enter expense in maintenance:"))
auto(insur,loan,gas,oil,tires,maint)
main()

15.def tax(val):
assm=val*0.6
taxx=assm*0.0072
print("The assessment value is:",assm)
print(f"The property tax is:{taxx:.3f}")
def main():
val=int(input("Enter value of the property:"))
tax(val)
main()
16.def cal(fat,carb):
fcal=fat*9
ccal=carb*4
print("The total calories for this day:",fcal+ccal)
def main():
fat=eval(input("Enter number of fat grams:"))
carb=eval(input("Enter number of carbohydrate grams:"))
cal(fat, carb)
main()

17.def sales(x,y,z):
a=x*20
b=y*15
c=z*10
tot=a+b+c
print("The total income generated is:",tot)
def main():
x=int(input("Enter number of class A seats sold:"))
y=int(input("Enter number of class B seats sold:"))
z=int(input("Enter number of class C seats sold:"))
sales(x,y,z)
main()

18.def paint(sp,gal):
ngal=sp/112
hrs=ngal*8
chrg=hrs*35
print("THe no of hours of labour required:",hrs)
print("The no of gallons of paint required:",ngal)
print("The cost of the paint:",ngal*gal)
print("The labor charges is:",chrg)
print("The total cost of paint job:",ngal*gal+chrg)
def main():
sp=int(input("Enter total wallspace required to paint:"))
gal=int(input("Enter cost per gallon of paint:"))
paint(sp,gal)
main()

19.def tax(sal):
sst=sal*0.05
cst=sal*0.025
tot=sst+cst
print("The amount of county sales tax:",cst)
print("The amount of state sales tax:",sst)
print("The total sales tax:",tot)
def main():
sal=int(input("Enter total sales for the month:"))
tax(sal)
main()

20.def feet_to_inch(feet):
inch=feet*12
return inch
def main():
feet=int(input("Enter value in feet:"))
print("In inches:",feet_to_inch(feet))
main()
21.import random
def main():
print("Printable Addition Test:\n")

for i in range(1, 6):


num1 = random.randint(1, 10)
num2 = random.randint(1, 10)

print(f"Question {i}")
print(f"{num1} + {num2} = _____")
print()
main()

22.def max(x,y):
if x>y:
return x
else:
return y
def main():
x=int(input("enter number 1:"))
y=int(input("enter number 2:"))
print("The maximum value is:",max(x,y))
main()
23.def det_grade(x):
if 100>x>90:
return 'A'
elif 80<x<89:
return 'B'
elif 70<x<79:
return 'C'
elif 60<x<69:
return 'D'
else:
return 'F'
def calc_average(t1,t2,t3,t4,t5):
avg=(t1+t2+t3+t4+t5)/5
return avg
def main():
t1=eval(input("Enter test score 1:"))
t2=eval(input("Enter test score 2:"))
t3=eval(input("Enter test score 3:"))
t4=eval(input("Enter test score 4:"))
t5=eval(input("Enter test score 5:"))
print("Grade for test 1:",det_grade(t1))
print("Grade for test 2:",det_grade(t2))
print("Grade for test 3:",det_grade(t3))
print("Grade for test 4:",det_grade(t4))
print("Grade for test 5:",det_grade(t5))
print("The average score is:",calc_average(t1,t3,t2,t4,t5))
main()

24.import random
while True:
x=random.randint(1,100)
c=0
while True:
g=eval(input("Guess a number between 1 to 100:"))
c+=1
if g>x:
print("Too high, try again.")
elif g<x:
print("Too low, try again.")
elif x==g:
print("Congratulations.Your guess was right!")
print("No of guesses:",c)
break

You might also like