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

1.using Conditionals and Loops: A) Using If Else

The document contains examples of using conditionals and loops in Python. It demonstrates using if/else statements to calculate employee wages based on hours worked (A), using else/if to determine a student's grade based on marks in multiple subjects (B), and using a while loop to play a guessing game of Hangman (C). Each example includes sample input/output to show how the code functions.
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)
41 views

1.using Conditionals and Loops: A) Using If Else

The document contains examples of using conditionals and loops in Python. It demonstrates using if/else statements to calculate employee wages based on hours worked (A), using else/if to determine a student's grade based on marks in multiple subjects (B), and using a while loop to play a guessing game of Hangman (C). Each example includes sample input/output to show how the code functions.
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/ 5

1.

USING CONDITIONALS AND LOOPS

A) USING IF ELSE

name=str(input(“Enter worker name:”))

hours=float(input(“Enter hours worked:”))

wage=float(input(“Enter dollars paid per hour:”))

if hours<=40:

totalwages=wage*hours

else:

overtime=hours-40

totalwages=wage*40+(1.5*wage)*overtime

print( “The worker:” +name+ ”total weekly wages is”)

print(totalwages)

OUTPUT:

Enter worker name: haritha

Enter hours worked: 78

Enter dollars paid per hour: 15

The worker: haritha total weekly wages is

1455.0

B) USING ELSE IF

m1=int(input(“Enter the first subject marks?:”))

m2=int(input(“Enter the second subject marks?:”))


m3=int(input(“Enter the third subject marks?:”))

m4=int(input(“Enter the fourth subject marks?:”))

if m1>85 and m1<=100 and m2>85 and m2<=100 and m3>85 and m3<=100 and m4>85 and
m4<=100:

print(“congrats! you scored grade A…”)

elif m1 >60 and m1<=85 and m2>60 and m2<=85 and m3>60 and m3<=85 and m4>60 and
m4<=85:

print(“you scored grade B+…”)

elif m1>40 and m1<=60 and m2>40 and m2<=60 and m3>40 and m3<=60 and m4>40 and
m4<=60:

print(“you scored grade B…”)

elif m1 >30 and m1<=40 and m2>30 and m2<=40 and m3>30 and m3<=40 and m4>30 and
m4<=40:

print(“you scored grade C…”)

else:

print(“sorry you are fail !”)

OUTPUT:

Enter the first subject marks?: 89

Enter the second subject marks?: 90

Enter the third subject marks?: 86

Enter the fourth subject marks?: 94

congrats! You scored grade A…

C) USING LOOPS

import time

name=input(“What is your name”)


print(“Hello,” +name, ”time to play hangman!”)

print(“ “)

time.sleep(1)

print(“start guessing…”)

word=”secret”

guesses= ’ ‘

turns=10

while turns>0:

failed=0

for char in word:

if char in guesses:

print(char)

else:

print(“_”)

failed += 1

if failed ==0:

print(“You won”)

break

print

guess = input(“Guess a character :”)

guesses += guess

if guess not in word:

turns -= 1

print(“wrong”)

print(“you have”, + turns ,’more guesses’)


if turns==0:

print(“you lose”)

OUTPUT:

What is your name : haritha

Hello, haritha time to play hangman!

Start guessing…

Guess a character: s

Guess a character: e

_
e

Guess a character: c

Guess a character: r

you won

You might also like