0% found this document useful (0 votes)
3 views

py ws 3

The document contains a series of Python code snippets that demonstrate various programming concepts such as conditional statements, loops, and user input handling. Each snippet addresses different tasks, including determining the quarter of a year, calculating shipping charges, and implementing a simple game of Rock, Paper, Scissors. The code examples are aimed at teaching fundamental programming skills and problem-solving techniques.

Uploaded by

unknownguy3023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

py ws 3

The document contains a series of Python code snippets that demonstrate various programming concepts such as conditional statements, loops, and user input handling. Each snippet addresses different tasks, including determining the quarter of a year, calculating shipping charges, and implementing a simple game of Rock, Paper, Scissors. The code examples are aimed at teaching fundamental programming skills and problem-solving techniques.

Uploaded by

unknownguy3023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Py ws 3

1.m=int(input("Enter the month:"))


if 1<=m<=3:
print("First quarter.")
elif 4<=m<=6:
print("second quarter")
elif 7<=m<=9:
print("THird quarter")
elif 9<=m<=12:
print("Fourth quarter.")
else:
print("Error in input.")

2.d=int(input("Enter the day in dd format:"))


m=int(input("Enter the month in mm format:"))
y=int(input("Enter the year in yy format:"))
if d*m==y:
print("Its a magic date!!")
else:
print("It is not a magic date!!")

3.ch=input("Enter one among a,b,c:")


if ch=='a' or ch=='A':
print("Apple!")
elif ch=='b' or ch=='B':
print("Banana!")
elif ch=='c' or ch=='C':
print("Coconut!")

4.hr=int(input("Enter hour:"))
c=int(input("Enter am(1) or pm(2):"))
hh=int(input("Enter how many hrs to go ahead:"))
if c==1:
nt=hr+hh
if(nt>12):
t=nt-12
print(t,' pm')
else:
print(nt,' am')
elif c==2:
nt=hr+hh
if(nt>12):
t=nt-12
print(t,' am')
else:
print(nt,' pm')

5.p=int(input("Enter number of people:"))


h=int(input("Enter no of hotdogs per person:"))
b=int(input("Enter no of buns:"))
toth=p*h
totb=p*b
nhp=toth//10
nbp=totb//8
nhr=toth-(nhp*10)
nbr=totb-(nbp*8)
print("The no of hotdog packages is:",nhp)
print("The no of hotdog buns pachages:",nbp)
print("The no of leftover hotdogs:",nhr)
print("The no of leftover hotdog buns:",nbr)
6. w=float(input("Enter the weight of the package:"))
if 0<w<2:
p=w*1.5
print("The shipping charges is:",p)
elif 2<=w<6:
p=w*3.0
print("The shipping charges is:",p)
elif 6<=w<10:
p=w*4.0
print("The shipping charges is:",p)
elif w>10:
p=w*4.5
print("The shipping charges is:",p)

7. print("Reboot the computer and try to connect.")


c=input("Did that fix the problem:(yes/no)")
if c=='no':
print("Reboot the router and try to connect")
d=input("Did that fix the problem:(yes/no)")
if d=='no':
print("Make sure the cables between router andnodes are plugged in firmly!")
e=input("Did that fix the problem:(yes/no)")
if e=='no':
print("Move the router to a new location.")
f=input("Did that fix the problem:(yes/no)")
if f=='no':
print("Get a new router")
8.
for i in range(200):
if i%5==2 and i%6==3 and i%7==2:
print("the total no of candies in the bowl is : ",i)
break

9.for i in range (1,21):


print(i," --- ",i*i,"\n")

1 --- 1

2 --- 4

3 --- 9

4 --- 16

5 --- 25

6 --- 36

7 --- 49

8 --- 64

9 --- 81

10 --- 100

11 --- 121

12 --- 144
13 --- 169

14 --- 196

15 --- 225

16 --- 256

17 --- 289

18 --- 324

19 --- 361

20 --- 400

10.n=int(input("Enter a number:"))
f=1
for i in range (1,n+1):
f=f*i
print("Factorial of",n,"is",f)

11.for i in range (100,1,-2):


print(i,",",end="")

13.n=int(input("Enter the high value"))


for i in range (1,n+1):
print(i*"*")
for i in range(n,0,-1):
print(i*"*")
.

14.n=int(input("Enter the high value"))


if n%2==0:
print("Diamond cant be made")
for i in range (1,n+1,2):
print(((n-i)//2)*" ", i*"*", ((n-i)//2)*" ")
for i in range(n-2,0,-2):
print(((n-i)//2)*" ", i*"*", ((n-i)//2)*" ")

15.sum=0
for i in range(0,31):
sum=sum+(i+1/31-i)
print("Thw sum of the series is:",sum)

16.cal=4.2
for i in range(10,31,5):
print(cal*i)

17.count=0
for i in range(1,6):
print("Enter the bugs for day ",i)
b=int(input())
count+=b
print("Total bugs collected is:",count)
Output:
Enter the bugs for day 1
2
Enter the bugs for day 2
3
Enter the bugs for day 3
4
Enter the bugs for day 4
5
Enter the bugs for day 5
6
Total bugs collected is: 20

18.c=0
n=1
while(c<50):

for i in range(2,n//2):
if n%i==0:
continue
else:
print(n,',',end="")
c+=1
if c%10==0:
print("\n")

19.
w=int(input("Enter your weight:"))
for i in range(1,16):
print("Your weight on moon after ",i," years")
w+=w*0.165
print(w)
print("The total weight after 15 years is:",w)
20.import math
for i in range(0,346,15):
print(i,"---",math.sin(i),math.cos(i))

Output:
0 --- 0.0 1.0
15 --- 0.6502878401571168 -0.7596879128588213
30 --- -0.9880316240928618 0.15425144988758405
45 --- 0.8509035245341184 0.5253219888177297
60 --- -0.3048106211022167 -0.9524129804151563
75 --- -0.38778163540943045 0.9217512697247493
90 --- 0.8939966636005579 -0.4480736161291701
105 --- -0.9705352835374847 -0.24095904923620143
120 --- 0.5806111842123143 0.8141809705265618
135 --- 0.08836868610400143 -0.9960878351411849
150 --- -0.7148764296291646 0.6992508064783751
165 --- 0.9977972794498907 -0.06633693633562374
180 --- -0.8011526357338304 -0.5984600690578581
195 --- 0.21945466799406363 0.9756226979194443
210 --- 0.46771851834275896 -0.8838774731823718
225 --- -0.9300948780045254 0.36731936773024515
240 --- 0.9454451549211168 0.32578130553514806
255 --- -0.5063916349244909 -0.8623036078310824
270 --- -0.1760459464712114 0.9843819506325049
285 --- 0.7738715902084317 -0.6333425312327234
300 --- -0.9997558399011495 -0.022096619278683942
315 --- 0.7451332645574127 0.6669156003948422
330 --- -0.13238162920545193 -0.9911988217552068
345 --- -0.5439958173735323 0.8390879278598296

21.while(1):
w=eval(input("Enter your weight in kg:"))
if w<0:
print("Weight cant be negative")
break
w=w*2.20
print("Your weight in pounds is",w)

22.while True:
print("Metric Conversion Menu:")
print("1. Pounds to Grams")
print("2. Grams to Pounds")
print("3. Inches to Centimeters")
print("4. Centimeters to Inches")
print("5. Kilometers to Miles")
print("6. Miles to Kilometers")
print("7. Exit")
ch=int(input("Enter choice:"))
if ch==1:
pounds=float(input("Enter pounds: "))
g=ppunds*453.592
print(f"{pounds} pounds is {(g):.2f} grams.")
elif ch==2:
g=float(input("Enter grams: "))
p=g/453.592
print(f"{g} grams is {p:.2f} pounds.")
elif ch==3:
i=float(input("Enter inches: "))
cm=i*2.54
print(f"{i} inches is {cm:.2f} centimeters.")
elif ch==4:
cm=float(input("Enter centimeters: "))
i=cm/2.54
print(f"{cm} centimeters is {i:.2f} inches.")
elif ch==5:
km=float(input("Enter kilometers: "))
m=km*0.6123
print(f"{km} kilometers is {m:.2f} miles.")
elif ch==6:
m=float(input("Enter miles: "))
km=m/0.6123
print(f"{m} miles is {km:.2f} kilometers.")
elif ch==7:
print("Exiting the program.")
break
else:
print("Invalid choice. Please select a valid option.")
23.n1=int(input("Enter number 1:"))
n2=int(input("Enter number 2:"))
if n1>n2:
max=n1
min=n2
else:
max=n2
min=n1
while True:
if min!=0:
r=max%min
max=min
min=r
elif min==0:
print("The GCD of the two numebrs is:",max)
break

24.import random as r
userp = aip = 0
ch = ["Rock", "Paper", "Scissors"]
print("""1 for Rock
2 for Paper
3 for Scissors""")
k =5
userw = [13, 21, 32]
aiw = [12, 23, 31]
for i in range(k):
userc =int(input("Enter : "))
if(userc not in [1,2,3]):
print("Wrong input")
k+=1
continue
aic = r.randint(1,3)

print(f"You chose {ch[userc-1]} and computer chose {ch[aic-1]}")


if(userc*10+aic in userw):
print("You won this round!!")
userp+=1;
elif(userc*10+aic in aiw):
print("Computer won :(")
aip+=1
else:
print("Its a Tie!!")
print("-"*15)
if(userp>aip):
print(f"You won the game!!!!:)[{userp}:{aip}]")
elif(aip>userp):
print(f"Computer won the game:([{userp}:{aip}]")
else:
print(f"Its a Tie :|[{userp}:{aip}]")
25.
a=float(input("Enter a number to find the square root: "))
x=a
y=(x+a/x)/2
while abs(x-y)>0.00001:
x=y
y=(x+a/x)/2
print(f"The square root of {a} is approximately {y:.5f}")

Output:

26.
n=int(input("Enter the number of teams in the tournament: "))
total=0
while n>1:
if n%2==0:
matches=n//2
n=n//2
else:
matches=(n-1)//2
n=(n-1)//2+1

total+=matches

print(f"The total number of matches played in the tournament is: {total}")

You might also like