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

Computer Science - Python Codes..

Uploaded by

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

Computer Science - Python Codes..

Uploaded by

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

#Codes that we learnt so far

#1. write a python code to add two numbers and print the total

#declaring variables and assigning a value


num1=5
num2=8
total=num1+num2
print("The total is", total)

#declaring variables to get user input


num1=int(input("Enter your first number:"))
num2=int(input("Enter your second number:"))
total=num1+num2
print("The total is", total)

#2. write a python code to compare two numbers and print the largest

#declaring variables and assigning a value


num1=20
num2=13
if num1>num2:
print("The largest number is", num1)
else:
print("The largest number is", num2)

#declaring variables to get user input


num1=int(input("Enter your first number:"))
num2=int(input("Enter your second number:"))
if num1>num2:
print("The largest number is", num1)
else:
print("The largest number is", num2)

#3. write a python code to compare three numbers and print the largest (this was
given as a H.W)

num1=int(input("Enter your first number:"))


num2=int(input("Enter your second number:"))
num3=int(input("Enter your third number:"))
if num1>num2:
if num1>num3:
print("The largest number is", num1)
else:
print("The largest number is", num3)
else:
print("The largest number is", num2)
#4. write a python code to find the sum of first 20 integers and print the total

#while loop
total=0
count=0
while count<21:
total=total+count
count=count+1
print("The total is", total)

#for loop
total=0
for count in range(0,21):
total=total+count
print("The total is", total)

#5. write a python code to find the sum of all even numbers between 0-10 and print
the total

#while loop
total=0
count=0
while count<11:
total=total+count
count=count+2
print("The total is", total)

#for loop
total=0
for count in range(0,11,2):
total=total+count
print("The total is", total)

#6. write a python code to find the sum of all odd numbers between 0-10 and print
the total

#while loop
total=0
count=1
while count<11:
total=total+count
count=count+2
print("The total is", total)

#for loop
total=0
for count in range(1,11,2):
total=total+count
print("The total is", total)

#7. write a python code for the guessing game (practice paper 1) (this was given as
a H.W)

number=20
guess=0
while guess!=20: #the != is for not equal
guess=int(input("Not matching, please guess again!"))
print("they match and you won!")

You might also like