Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CMP 211

ACCEPTING USER INPUT


input()

x = input()
print (x)

x = input('What is your name? ')


print ('hello', x + ', please to meet
you.')

CONDITIONAL STATEMENTS
- The IF Statement
if x > y:
print (x, 'is greater than', y)
x
z = 5

if z <= x :
print (z, 'is less than or eqaul to',
x)

if z == x :
print (z, 'is eqaul to', x)

if z != y :
print (z, 'is not eqaul to', y)
if z < y > x:
print (y, 'is greater than', z, 'and
greater than', x)

a = 3

if z < y > x > a :


print ( y, 'is greater than ...')

- The IF ELSE
x = 5
y = 8

if x > y:
print (x, 'is greater than', y)
else:
print (x, 'is not greater than', y)

What'll be the output;


if x > y:
print (x, 'is greater than', y)
if x > 55:
print (x, 'is greater than', 55)
else:
print (x, 'is not greater than', y)

- The IF ELIF ELSE


x = 5
y = 10
z = 22

if x > 5:
print (x, 'is greater than', y)
elif 5 > 2:
print ('5 is greater than 2')

x = 5
y = 10
z = 22

if x > 5:
print (x, 'is greater than', y)
elif x < z:
print (x, 'is less than', z)
elif 5 > 2:
print ('5 is less than 2')
else:
print('if and elif(s) never ran')

EXERCISES
1. Write program that will accept an age (of a voter), it will
check if the voter can vote or not. If the voter is 18 years
or over, the program will display "You can vote", if the
voter is less than 18, it will display, "Sorry, you are not
eligible to vote".
Answer
x = int (input ('Enter your age: '))
if x < 18:
print ('You are not eligible to vote')
else:
print ('You are eligible to vote')

2. Write a program that will accept two numbers from


the user and checks if their sum is less than 50.
Answer
x = int(input('Enter the first number:
'))
y = int(input('Enter the second
number: '))
if x + y < 50:
print(x + y, 'is less than' ,50)
else:
print(x + y,'is greater than',50)
Write a program that will accept two numbers from the
user and check if the second number is a factor of the
first number.

x = int (input('Enter the first


number: '))
y = int (input('Enter the second
number: '))
if y % x == 0 :
print(y, "is a factor of" ,x)
else:
print(y, "is not a factor" ,x)

Write a currency converter program. The program will ask


the user if he wants to convert from naira to dollars or from
dollars to naira. Based on the user input, the program will
then collect a value from the user and convert to the user
selected currency.

print('Welcome to currency converter')


print('Enter 1: To Convert from Naira to
Dollar')
print('Enter 2: To Convert from Dollar to
Naira' )
Converter = int(input(':'))
if Converter == 1:
print('Welcome to Naira to Dollar
Converter')
Naira = float(input('Enter the naira
amount please:N '))
Dollar = float(Naira / 306)
print('Amount in Dollar: $' ,
str(Dollar))
elif Converter == 2:
print('Welcome to Dollar to Naira
Converter')
Dollar = float(input('Enter the
dollar amount please:$ '))
Naira = float(input(Dollar * 306))
print('Amount in Naira:N ',
str(Naira))

You might also like