Uploaded by social social

Review of Selected Programming Skills for Exam 11

advertisement
Review of Selected Programming
Concepts and Skills
Today's Date: Monday February 13, 2023
Exam 1 Date: Thursday February 16, 2023 In class
Skills for Exam 1 Programs
• Ability to work from a problem statement to a working program
solution which satisfies all requirements.
• Ability to use input and print.
• Use end and sep in print.
• Use of format with print including format specifiers such as 10.2f
• Understand how to import libraries such as math and random.
• Be able to use sqrt and pi from math.
Skills for Exam 1 Programs
•
•
•
•
•
Be able to use randrange and randint from random.
Use if-elif-else and similar selection structures.
Use for loops with range and while loops.
Understand and use Boolean, int, float, and string data types
Be able to structure a program with functions: main and
several other that you create: pass and return variables.
• Use of global variables and constants.
x = 2.323
y = 123
z = 'Hi Bob!'
print('The number is: ', format(x,'5.6f'))
# Our textbook
# More general form
print('{0:5.6f} and and {2} and {1:10d} and {2}'.format(x,y,z))
'''
The number is:
2.323000
123456
2.323000 and and Hi Bob! and
'''
123 and Hi Bob!
1234567890
Data Types and Conversion:
>>> x = 1
>>> y = 3
>>> z = x/y
>>> type(x) # What does this do?
What are the data types of x, y, z?
>>> u = 'Hello '
>>> v = 'World'
>>> w = u + v
>>> type(w) # What does this do?
What is w? What are the data types of u, v, w
Python Operators
•
•
•
•
Arithmetic operators.
Assignment operators.
Comparison operators.
Logical operators.
Arithmetic Operators
User Created Functions
# Comments
def main():
print('Hi')
x = get_xx( )
y = square_x(x)
addxy(x,y)
good_bye()
def get_xx():
x = float(input('Enter x:'))
return x
def square_x(z):
return z*z
def addxy(p,q):
print('Sum is:',p+q)
def good_bye():
print('Good bye!')
main()
''' output goes here
'''
Submit myprog.py
and myprog.txt to
Dropbox for Exam 1
A certain small Wright State class has mostly seniors and graduate students of various ages.
You are asked to write a program which computes the average age to the students. The main
function inputs and computes the average age and an additional function, make_comment,
then computes an output statement regarding the class size. (15 min max)
def main():
# while loop inputs ages of students and adds up the total.
# An age input of 0 stops the loop.
# Average age computed next outside the while loop and printed
# Call to make_comment.
make_comment(class_size)
def make_comment(size):
# If the class has 10 or less print "small class"
# Otherwise print "large class" is more than 10.
Submit fileprefixtryexam1.py
and txt to the Dropbox
The next slide is the solution code! Don't look at it until you are sure you are finished trying
to code the problem. I recommend you time yourself – see how far you get in 15 minutes
and then upload to the Exam 1 Practice Dropbox. Speak up in class or email me if you have
questions about the solution code or your submittal. RTaylor ronald.taylor@wright.edu
# Comments
def main():
count = 0
total = 0
age = int(input('Enter age:'))
while age > 0:
total += age
count +=1
age = int(input('Enter age:'))
average = total/count
print('average age = ',format(average,'5.2f'))
print('there are {} students in the class'.format(count))
make_comment(count)
def make_comment(number):
if number <= 10:
print('Small class')
else:
print('Large class')
main()
'''
Enter age:20
Enter age:20
Enter age:20
Enter age:20
Enter age:20
Enter age:20
Enter age:20
Enter age:30
Enter age:30
Enter age:30
Enter age:0
average age = 23.00
there are 10 students in the class
Small class
'''
Download