Presentation is loading. Please wait.

Presentation is loading. Please wait.

What we learned so far Ask a question, and get a user input (in characters) If..else While for.

Similar presentations


Presentation on theme: "What we learned so far Ask a question, and get a user input (in characters) If..else While for."— Presentation transcript:

1 What we learned so far Ask a question, and get a user input (in characters) If..else While for

2 Ask what the name is Ask what the major is Ask when you started the school Print “My name is …. studying... from....” >>> print(“What is your name?”) >>> myName = input() Kim >>> print (“What is your major?”) >>> myMajor = input() CS >>> print (“When did you start the school?” >>> myStartYr = input() 2000 >>> print(‘My name is ‘ + myName + ‘ studying ‘ + myMajor + ‘ from ‘ + myStartYr) Past Lab 0907

3 Boolean Operators/Test if …. else.... def main(): myAge = input(‘What is your age\n’) if int(myAge) <= 0 : print(‘Your age has to be positive’) else: print(‘I will be ‘ + str(int(myAge)+5) +’ years old in 5 years’) main()

4 Guess name and password Within 5 trials, guess the name ‘kim’ and password def logIn(): count=0 while count < 5: count= count+1 name=input(‘Enter a name.’) if name !=‘kim’: continue passwd = input(‘What is password?’) if passwd == ‘xxx’: break if count < 5: print(‘Access granted’) else: print(‘Five attempts failed.’)

5 For loop Convenient when the number of iterations is known for count in range(5): name=input(‘Guess my name.\n’) if name==‘kim’: break count=0 while count< 5: name=input(‘Guess my name.\n’) if name==‘kim’: break count= count+1

6 Example: Compute a class average Liberal Arts Level Add grades and divide by the number of students What more do we need Data Process/procedure Output

7 Computer Science Design Level Initialize total to zero Initialize counter to zero Get the first grade while not the end of grades add this grade into the running total add one to the grade counter input the next grade if the counter is not equal to zero computer total divided by the counter print the average else print 'no grades were entered' Example: Compute a class average

8 total = 0 counter = 0 while grades[counter] >=0: total = total + grades[counter] counter = counter+1 if counter > 0: average = total/counter print average else: print 'no grades were entered' Computer Science Coding Level Example: Compute a class average

9 total = 0 counter = 0 while grades[counter] >=0: total = total + grades[counter] counter = counter+1 if counter > 0: average = total/counter print average else: print 'no grades were entered' Design vs. Coding Level Initialize total to zero Initialize counter to zero Input the first grade while not the end of grades add this grade into the running total add one to the grade counter input the next grade if the counter is not equal to zero computer total divided by the counter print the average else print 'no grades were entered'

10 Lab 0930 Create a list of grades: grades = [90,85,….] # of grades in grades[] can be found by numG = len(grades) Write a Python program to compute the average of grades[] and print the average Email the program to COMP1000.201@gmail.com


Download ppt "What we learned so far Ask a question, and get a user input (in characters) If..else While for."

Similar presentations


Ads by Google