Coding Revision

You might also like

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

HIT137 Tutorial Revision

Week 1 to 4
Question 1:
Write a Python program to find the smallest and largest of three
numbers.
Sample Input: Enter the three numbers: 1,2,3
Sample Output: Largest:3 Smallest:1

Solution:
#this program will find the smallest and largest number of 3 numbers
entered

first=int(input ("enter the first number "))#user will input number


second=int(input ("enter the second number "))#user will input number
third=int(input ("enter the third number "))#user will input number

print ("maximum", max (first,second,third))#in built function


print ("minimum", min (first,second,third))#in built function

Question 2:
Write a python program that will print the following:
1 3 5 7 9 11
10 8 6 4 2 0
(Hint: After 11 the number gets subtracted by 1 then it gets
subtracted by 2)

Solution 1:
Firstnumberset = 1
#for loop, to run 6 numbers adding 2 each time
for eachPass in range(6):
print(Firstnumberset, end=" ")
Firstnumberset = Firstnumberset + 2

print("\n") #line break creation

#for loop, to run 6 numbers subtracting 2 each time


SecondnumberSet = 11-1
for eachPass in range(6):
print(SecondnumberSet, end=" ")
SecondnumberSet = SecondnumberSet - 2
print("\n") #line break creation
Solution 2:
for top in range(1,12,2):
print(top, end=" ")
print("\n")
for bot in range(10,-1,-2):
print (bot, end=" ")

Question 3:
Write a python program that will accept 10 numbers from the user and
find the number of odd and even numbers.
Sample Input: 22, 11, 6, 99, 100
Sample Output: There are 2 odd numbers and 3 odd numbers

Solution:
even=0
odd=0

for eachPass in range(10):


number=int(input("Enter number "))
if(number % 2) == 0:
even+=1
else:
odd+=1
print("There are", odd, "odd numbers.")
print("There are", even, "even numbers.")
Question 4:
Write a python program that will print the number of days if the user
inputs the month name. Sample Input: March Sample Output: No of days:
31.

Solution 1:
Days28=['February']
Days30=['April', 'June', 'September', 'November']
Days31=['January', 'March', 'May', 'July', 'August', 'October',
'December']

month=input("Please input 1 of the 12 months of the year : ")

if month in (Days28):
print(month, "has 28 Days excluding leap years.")
if month in (Days30):
print(month, "has 30 Days.")
if month in (Days31):
print(month, "has 31 Days.")

else:
print("Please input a valid month.")

Solution 2:
x=str(input("Please enter the month name: "))
if x=="February":
print("Number of days: 28/29 days")
elif x in ("April","June","September","November"):
print("Number of days: 30")
elif x in ("January","March","May","July","August","October",
"December"):
print("Number of days: 31")
else:
print("Please type the correct month name")
Question 5:
Write a program that takes input from the user and when the user press
enter to quit the program gives the sum of all even numbers and the
sum of all odd numbers.
Sample Input:
Enter a number 2
Enter a number 4
Enter a number 3
Enter a number 5
Enter a number (enter)
Output
Sum of even number: 6
Sum of Odd number: 8

Solution 1:
#sums the even and odd numbers
#Create variables
Evennumber=0
Oddnumber=0
Userinput=0 # User input

#Use while, if and else functions to verify inputs are even,odd or


none

while Userinput!="": # If user hits enter without a number input


Userinput = input("Please enter a number: ")
if Userinput !="": #User enters a number as requested
sum= int(Userinput)
if sum %2 ==0: #modulus to verify even numbers
Evennumber= Evennumber+sum
else: #if the if result is false and the number is Odd number
Oddnumber = Oddnumber+sum
print("\n")

print("The sum of the even numbers is: ", Evennumber)


print("The sum of the odd numbers is: ", Oddnumber)
Solution 2:
sum_even=0 #initialise sum_even variable
sum_odd=0 #initialise sum_odd variable
data=input("enter a number or just enter to quit \t")#obtain data
while data !="": #create while loop for user input
number=float(data) #convert user input to a float
if number%2!=0: #calculate whether number is odd or even
sum_odd+=number #if odd, add to the sum_odd
else:
sum_even+=number #if even, add to the sum_even
data=input("enter a number or just enter to quit \t")#get input

print("Sum of the odd numbers is ", sum_odd, "\nSum of the even


numbers is ", sum_even) #display total sums

Question 6:
Write a program to check whether the user input number is a prime
number or not.
Sample:
Input
Enter number: 8
Output
Number 8 is not prime number.

Solution:
n = int(input("Please enter a whole number: "))
if n ==1: # is the number is one, exit the script
print("1 is not a prime number.")
else:
for i in range (2,n):
if n%i == 0: #check if divisible with a remainder
print(n, "is not a prime number.") # no remainder
break
elif i == n-1: #always a remainder, not divisible
print(n, "is a prime number.")
Question 7:
Write a program to print the numbers in the following format:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Solution:
a=[]
for i in range(1,6):
a.append(i)
for j in range(len(a)):
print(a[j], end=" ")
print('\n')

Question 8:
Write a program that accepts a sentence and calculate the number of
letters and digits. Suppose the following input is supplied to the
program:
hello class! 123
Then, the output should be:
LETTERS 10
DIGITS 3

Solution:
sentence=input("Enter any sentence:")
letters=numbers=0
for x in sentence:
chvalue=ord(x)
if (chvalue>=65 and chvalue<=90)or(chvalue>=97 and chvalue<=122):
letters+=1
elif chvalue>=49 and chvalue<=57:
numbers+=1
print("LETTERS",letters)
print("DIGITS",numbers)

You might also like