Lab 2

In this lab, it will be a little different from lab 1. You will learn how to use python  in script form. Python script is where you can store codes in a file and then use the interpreter to execute the file. In scripts, you type in the codes by hand. First find and open your python. Then go to ‘file’, on the top left hand corner and go to new window. Once you opened a new window, it will show a blank page. Now we have to find the sum of x and y, so type in option = 1, then press enter. Then type in while (option ==1): and enter. Here you will see that the “while” is in a different color, that will help execute, “option = 1”. Now press enter, notice that your next line has been indented. Then type option = input (“Menu (1) Enter numbers (2) Exit”). In this command, it will show up when you execute it later on. Press enter then type in #print option. Press enter again and type in, if (option == 1): . Now in order for this this script to work, you need a input. So now press enter again and you’ll see that the next space will be double indented. Once you see that it has double indented, type x = input (“Enter x “), press enter and the same thing but replace x with y. So it will look something like, y = input (“Enter y “). Press enter, then when your looking for sum, you would type z = x +y. But in difference, you would subtract and in product, you would use the * button. Also as well with other things like quotient, you would need to use the divide button. Now for the finally step, you would press enter again and type, print “x + y =”, z. The signs will have to be changed depending on what you are looking for.( addition, subtraction,divide, etc…) Now press f5 and it will as you if you want to save or not. Save it into a file that will end with, .py. If the it is not saved as a .py file, then it will not run properly on python that’s because python will not be ab;e to read it. After you have saved, python will run again but this time, it will show up something like this, Menu (1) Enter numbers (2) Exit. Remember you don’t want to press enter before you type 1 because python will not understand and you’ll get an error and have to run the script again. Now type 1 and it will show x, that will be your input, press enter again, it will show y. Once you typed in numbers for both inputs, you’ll get you answer. Repeat these steps five more times but with different signs.

Source code:

The sum of x and y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x + y
print “x + y =”, z

The difference of x and y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x – y
print “x – y =”, z

The product of x and y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x * y
print “x * y =”, z

The quotient of x and y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x/y
print “x/y =”, z

The remainder of x and y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x%y
print “x%y =”, z

Whether x is less than, equal, or greater than y

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x<=y
print “x<=y =”, z

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x==y
print “x==y =”, z

option = 1
while (option == 1):
option = input(“Menu (1) Enter numbers (2) Exit”)
#print option
if (option == 1):
x = input (“Enter x “)
y = input (“Enter y “)
z = x>=y
print “x>=y =”, z

 

lab 2 screen shot

 

Leave a Reply

Your email address will not be published. Required fields are marked *