Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Programming Essentials

exercise

Uploaded by

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

Programming Essentials

exercise

Uploaded by

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

Programming Essentials (Python)

Conditional Logic or Selections or Decisions


Exercise 1

1. Write a program which reads two integer values. If the first number is less than
the second, print the message "First number is less than the second".
If the second is less than the first, print the message "Second number is less
than the first".
If the numbers are equal, print the message "The numbers are equal".

Program :

first_number = int(input("Enter the first number: "))


second_number = int(input("Enter the second number: "))
if first_number < second_number:
print("first number is less than the second")
elif second_number < first_number:
print("second number is less than the first")
else: print("the numbers are equal")

Result :

2. Write a program that prompts the user to enter age. If age of the user is equal to
or greater than 18, then the proram displays “You are eligibel to vote”. Otherwise
the program displays “You cannot vote”.

Program :

age = int(input("Please enter your age: "))


if age >= 18:
print("You are eligible to vote")
else: print("You cannot vote")
Result :

3. The Young and Beautiful Travel Company restricts its clients to ages between 18
and 30. Write a program to input a client's age and test whether they are eligible
to go on vacation with the company or not.

Program :

age = int(input("Please enter your age: "))


if 18 <= age <= 30:
print("You are eligible to go on company vacation")
else: print("You are not eligible to go on company vacation")

Result:

4. Write a program to input a salary from the user and determine how much tax
someone should pay according to the following rules:
People pay no tax if they earn up to €10,000. They pay tax at the rate of 20% on the
amount they earn over €10,000 but up to €50,000. They pay tax at 40% on any money
they earn over €50,000.

Program:

salary = float(input("please enter your salary: "))


tax = 0.0
if salary <= 10000:
tax = 0.0
elif salary <= 50000:
tax = (salary - 10000) * 0.20
else: tax = (50000 - 10000) * 0.20 + (salary - 50000) * 0.40
print(f"the tax you should pay is: €{tax:.2f}")

Result:

5. Write a program that prompts the user for either 1, 2, 3 or 4.


In your python code you will need to print out what 1, 2, 3 and 4 correspond to
using below:
1 = diamonds, 2= hearts, 3= clubs and 4 = spades respectively.

Program:

choice = int(input("Enter a number (1,2,3,4): "))


if choice == 1:
print("Diamonds")
elif choice == 2:
print("Hearts")
elif choice == 3:
print("clubs")
elif choice == 4:
print("spades")
else: print("Invalid input. Please enter 1,2,3,4")

Result:
6. Write a program which displays a menu to the user. The menu should look
similar to the following:

Welcome to the SimpleCacl


Press 1 to add two numbers
Press 2 to subtract two numbers
Press 3 to divide two numbers
Press 4 to multiple two numbers
Read in the selection from the user and then ask the user for two numbers. Print the
result to the user.

Program:

print("Welcome to the SimpleCalc")


print("Press 1 to add two numbers")
print("Press 2 to subtract two numbers")
print("Press 3 to divide two numbers")
print("Press 4 to multiply two numbers")

choice = input("Enter your choice (1-4): ")

if choice in ['1', '2', '3', '4']:


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

if choice == '1':
result = num1 + num2
print(f"The result of adding {num1} and {num2} is {result}")
elif choice == '2':
result = num1 - num2
print(f"The result of subtracting {num2} from {num1} is {result}")
elif choice == '3':
if num2 != 0:
result = num1 / num2
print(f"The result of dividing {num1} by {num2} is {result}")
else:
print("Error: Division by zero is not allowed.")
elif choice == '4':
result = num1 * num2
print(f"The result of multiplying {num1} and {num2} is {result}")
else:
print("Invalid choice. Please choose a number between 1 and 4.")

Result:

You might also like