Q1. Simple Calculation with Type Conversion¶
Ask user for hours and rate, calculate gross pay
Handle invalid input using try/except
In [198]:
hours = input("Enter Hours: ")
rate = input("Enter Rate: ")
try:
hours = float(hours)
rate = float(rate)
pay = hours * rate
print("Gross Pay:", pay)
except:
print("⚠ Error: Please enter numeric value")
Gross Pay: 25.0
Out[198]:
105.0
In [56]:
try:
(hours,rate) = map(float,[input("Enter hours"),input("Enter rate")])
print(hours*rate)
except ValueError:
print("Error:Please enter numeric value")
10.0
In [49]:
try:
(hours,rate) = map(float,input( "Enter hours and rate separated by space").split())
print(hours*rate)
except ValueError:
print("⚠ Error: Please enter two numeric values separated by space")
⚠ Error: Please enter two numeric values separated by space
In this code Pay = string & 2*Rate = Numeric value & In python we can't add string & numeric value.¶
In [84]:
print("Pay:" +2 *rate) # ❌ Buggy line
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[84], line 1 ----> 1 print("Pay:" +2 *rate) TypeError: can only concatenate str (not "int") to str
We can use , or * between string & numeric values¶
In [82]:
rate = 10
print("Pay:" ,2 *rate)
print("Pay:" *2 *rate)
Pay: 20 Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:Pay:
f-strings make it easy to build dynamic text by embedding variables or expressions directly into a string using {}¶
f-strings(formatted strings)
In [78]:
rate = 2.5
print(f"Pay:{2 * rate}")
Pay:5.0
In [86]:
rate = 50
print(f"Square of rate: {rate**2}, Double: {2 * rate}")
Square of rate: 2500, Double: 100
Q3. Predict the Output¶
What will this print?
In [87]:
x = 5
x = x + 2
print(x) # 👉 Your prediction: 7
7
Q4. Debugging Practice¶
Identify and fix 2 problems in this code
In [113]:
# name = input("Enter your name")
# print("Hello" name) # Syntax Error
# Fixed:
name = input("Enter your name: ")
print("Hello", name)
Hello Lara
Q5. Multiple Variable Assignments¶
Swap values of two variables without using a third variable
In [118]:
a = 10
b = 20
# TODO: Swap the values
# After swap, a = 20, b = 10
print(f"a: {a}, b: {b}")
a: 10, b: 20
Q6. Advanced Input Parsing¶
In [124]:
inp = input("Enter Hours & rate separated by space: ")
Hours,rate = inp.split()
Hours = float(Hours)
rate = float(rate)
pay = Hours * rate
print("pay:", pay)
pay: 8.0
In [132]:
try:
hours,rate = map(float,[input("Enter hours: "),input("Enter rate: ")])
print(hours*rate)
except ValueError:
print("Error!: Please Enter numeric value only.")
10.0
In [133]:
hours,rate = map(float,input("Enter hours and rate separated by space: ").split())
pay = hours * rate
print(pay)
40.0
In [145]:
try:
hours,rate = map(float,input("Enter hours and rate separated by space: ").split())
print(hours*rate)
except ValueError:
print("Error!: Enter pair of numeric values separated by space only!")
14.4825
In [137]:
Pencils,Rate = map(float,input("Enter Pencils and Rate per pencil separated by space").split())
print(Pencils*Rate)
50.0
In [143]:
try:
Pencils,Rate = map(float,[input("Enter no. of Pencils: "),input("Enter Rate: ")])
print(Pencils*Rate)
except ValueError:
print("Error!: Please enter numeric value only!")
32.0
Q7. Check Types and Do Arithmetic¶
In [156]:
x = float(input("Enter a number: "))
print("Before conversion:", type(x))
x = float(x)
print("Square:", x ** 2)
Before conversion: <class 'float'> Square: 25.0
In [182]:
try:
x = float(input("Enter value of x: "))
print(f"Before conversion: {type(x)}")
print(f"Square: {x**2}")
except ValueError:
print("Error!: Please enter numeric value only!")
Error!: Please enter numeric value only!
Q8. Dynamic Message Generator¶
In [184]:
name = input("Enter your name: ")
subject = input("Enter subject: ")
print(f"Hello {name}, welcome to {subject}!")
Hello Lara, welcome to Python programming!
Q9. Complex Expression Evaluation¶
In [185]:
expr = 10 + 5 * 2 - 3 / 2
print("Result:", expr) # Because of operator precedence: *, / first then +, -
Result: 18.5
Q10. Mini Project: Simple Tip Calculator¶
In [190]:
bill = float(input("Enter bill amount: "))
tip_percent = float(input("Enter tip %: "))
people = int(input("Split between how many people?"))
tip = bill * (tip_percent/100)
total = bill + tip
per_person = total/people
print("Tip amount:" , round(tip,2))
print("Total per person:" , round(per_person,2))
Tip amount: 25.0 Total per person: 131.25
In [197]:
try:
bill,tip_percent = map(float,[input("Enter bill amount: "), input("Enter tip %: ")])
people = int(input("Split between how many people?"))
tip = bill*(tip_percent/100)
total = bill + tip
per_person = total/people
print("Tip amount:", round(tip,2))
print("Total per person:", round(per_person,2))
except ValueError:
print("Error: Please enter integer value for people!")
Tip amount: 50.0 Total per person: 105.0
In [ ]: