A453 Task 1: Analysis: Problem: Solution:
|
|
- Chastity Bradley
- 5 years ago
- Views:
Transcription
1 : Analysis: Problem: The problem I need to solve is that I need to design, code, and test a program that simulates a dice throw of a 4, 6, or 12 sided die and outputs the result before repeating the process again for as many times as desired by the user. Solution: I can solve this problem by identifying the steps needed for a program to perform this process and then programming those steps in Python 3. Success Criteria: The success criteria of this program would that: would have the user able to input which of the dice is thrown. must generate a random number between 1 and the user s input. output the amount of sides on the die and the number it has landed on. The user able to repeat the process as many times as they want. able to simulate a 4 sided die. able to simulate a 6 sided die. able to simulate a 12 sided die. able to identify incorrect die-number input. Testing: I'll test the program by running it and giving different inputs when asked what numbered die I d like to select and whether to see if the expected outcomes are correct and then putting the results in a testing table with a column explaining what I'm going to do, then a column explaining the expected outcome, and a third column explaining the actual outcome. 1
2 Design: Variables: 1.continued: This is a boolean variable that is initially set to true. The rest of the code is contained in a while loop that will repeat until continued is set to false. It is set to false when the user decides not to continue when asked by the program. 2.sides: This is a string variable that is set to the users' input when the program asks them how many sides they want the die to have. 3.sidesint: This is an integer variable that is set to an integer form of the value of the sides variable. 4.result: This is an integer variable that is set to a random number between 1 and the maximum number possible of the chosen die. Functions: 1. randint: a function stored in the random class that has 2 integer variable inputs. The function outputs a random number between these 2 numbers. 2. print: a function that outputs a given string value. 3. input: a function that returns a string value from the user s input after it outputs a given string value. 4. lower: a function that returns the value of a given string but with all letters being lowercase. 5. int: a function that returns the value of a given value that has been converted to an integer. 2
3 Flow Chart: 3
4 Validation: Validation occurs when the user is asked which of the dice to pick as any input that isn't digits of either 4, 6, or 12 is disregarded and the user is asked for the input again. It also occurs when the user is asked whether to continue or not as any input that isn't text that says yes or no is disregarded and the user is asked for the input again. 4
5 Draft Code solutions: First Coding attempt: #Dice simulator for a 4,6 and 12 sided die print("welcome to my die simulator") import random loop = 23 while loop!= 22: sides = input("what sided dice do you want to roll a 4,6 or 12 sided die?") if sides=="4": result=random.randint(1,4) print("the number that the dice has landed on is",result) elif sides=="6": result=random.randint(1,6) print("you rolled a 12 sided dice and the number that the dice has landed on is",result) elif sides=="12": result=random.randint(1,12) print("you rolled a 12 sided dice and the number that the dice has landed on is",result) else: print("i m sorry, you can only roll a 4,6 or 12 sided dice in this game") This attempt was not acceptable as if the user could enter a letter instead of a number and cause IDLE to throw an exception and the user could also enter a different number to the ones that the program accepts. Also, it doesn t allow the user to keep on rolling the die as many times as they desire. 5
6 Second Coding attempt: #Dice simulator for a 4,6 and 12 sided die print("welcome to my die simulator") import random continued = True while continued == True: sides = input("what sided die do you want to roll, a 4,6 or 12 sided die?") sidesint = int(sides) while sidesint!= 4 and sidesint!= 6 and sidesint!= 12: sides = input("i'll ask you again! What sided die do you want to roll, a 4, 6 or 12 sided die?") if sidesint == 4: result=random.randint(1,4) print("the number that the dice has landed on is",result) elif sidesint == 6: result=random.randint(1,6) print("you rolled a 12 sided dice and the number that the dice has landed on is",result) elif sidesint == 12: result=random.randint(1,12) print("you rolled a 12 sided dice and the number that the dice has landed on is",result) else: print("i m sorry, you can only roll a 4,6 or 12 sided dice in this game") check = input("would you like to continue? Yes or no!") check = check.lower() while check!= "yes" and check!= "no": check = input("i'll ask you again! Would you like to continue? Yes or no!") if check == "no": continued = False print("ahh well!") else: print("yay") This attempt was not acceptable as although I ve added in code to loop the program as long as the user desires (on line 5 I have started a while loop that will continue as long as the value of continued is True, and from line 21 onwards, I ve 6
7 asked the user whether they want to continue and then set the value of continued depending on that decision) and I ve added code to make sure that the user can t input an incorrect number (between lines 6 and 9, I ve asked the user what numbered die they would like to roll and if the input isn t. It still doesn t deal with the possibility of the user not inputting numbers. Final Code: #Dice simulator for a 4,6 and 12 sided die print("welcome to my die simulator") #Show the text "Welcome to my die simulator" import random #import all resources (functions) from random library continued = True #Create a boolean variable called "continued" and set its' value to True. while continued: #Loop following indented code as long as the value of continued is True. sides = input("what sided die do you want to roll, a 4,6 or 12 sided die?") #Create a string variable called "sides" and set the value to the users' input after the text "What sided die do you want to roll, a 4, 6, or 12 sided die?" try: #Attempt the following indented code. sidesint = int(sides) #Create an integer variable called "sidesint" and set its' value to the value of "sides" converted to integer. except: #If the code inside the try statement fails, carry out the following indented code instead. sidesint = 0 #Create an integer variable called sidesint and set its' value to 0. while sidesint!= 4 and sidesint!= 6 and sidesint!= 12: #Loop following indented code as long as the value of sidesint os not equal to 4, 6, or 12. sides = input("i'll ask you again! What sided die do you want to roll, a 4, 6 or 12 sided die?") #Set the value of "sides" to the users' input after the text "I'll ask you again! What sided die do you want to roll, a 4, 6, or 12 sided die?". try: #Attempt the following indented code. sidesint = int(sides) #Set the value of "sidesint" to the value of sides as an integer. except: #If the code in the try statement fails, carry out the following indented code instead. 7
8 sidesint = 0 #Set the value of "sidesint" to 0. if sidesint == 4: #If the value of sidesint is 4, carry out the following indented code. result=random.randint(1,4) #Create an integer variable called result and set its value to a random number between 1 and 4. print("you rolled a 4 sided die and the number that the die has landed on is",result) #Show the text You rolled a 4 sided die and the number that the die has landed on is followed by the value of result. elif sidesint == 6: #If the previous if statement s parameters are not met and the value of sidesint is 6, carry out the following indented code: result=random.randint(1,6) #Create an integer variable called result and set its value to a random number between 1 and 6. print("you rolled a 6 sided die and the number that the die has landed on is",result) #Show the text You rolled a 6 sided die and the number that the die has landed on is followed by the value of result. elif sidesint == 12: #If the previous if statements parameters are not met and the value of sidesint is 12, carry out the following indented code: result=random.randint(1,12) #Create an integer variable called result and set its value to a random number between 1 and 12. print("you rolled a 12 sided die and the number that the dice has landed on is", result) #Show the text You rolled a 12 sided di e and the number that the die has landed on is followed by the value of result. else: #If the previous if statements parameters are not met, carry out the following code: print("i m sorry, you can only roll a 4, 6 or 12 sided die in this game") #Show the text I m sorry, you can only roll a 4, 6, or 12 sided die in this game check = input("would you like to continue? Yes or no!") #Create a string variable called check and set its value to the user s input when asked Would you like to continue? Yes or no! check = check.lower() #Change the value of check so that all its letters are lowercase. while check!= "yes" and check!= "no": #Loop following indented code as long as the value of check is not equal to yes or no check = input("i'll ask you again! Would you like to continue? Yes or no!") #Create a string variable called check and set its value to the user s input when asked I ll ask you again! Would you like to continue? Yes or no! 8
9 if check == "no": #If the value of check is equal to no, carry out the following indented code: continued = False #Set the value of continued to False. print("ahh well!") #Show the text Ahh well! else: #If the previous if statement s parameters are not met, carry out the following code: print("yay") #Show the text Yay Testing: Testing Table: Test number: 1 Description: Test to see if the 4 sided die works by choosing it when prompted. Test data: 4 Technique: When asked What sided die do you want to roll, a 4, 6 or 12 sided die? I ll input the test data (which is 4). Expected Result: text saying You rolled a 4 sided die and the number the die landed on is followed by a random number between 1 and 4. Actual Result: has that says You rolled a 4 sided die ant the number that the die landed on is 4 which is just as predicted, with the ending 4 being a random number between 1 and 4 9
10 2 Test to see if the 6 sided die works by choosing it when prompted. 6 When asked What sided die do you want to roll, a 4, 6 or 12 sided die? I ll input the test data (which is 6). text saying You rolled a 6 sided die and the number the die landed on is followed by a random number between 1 and 6. that says You rolled a 6 sided die and the number that the die has landed on is 5 which is just as predicted, with the ending 5 being a random number between 1 and 6. 3 Test to see if the 12 sided die works by choosing it when prompted. 12 When asked What sided die do you want to roll, a 4, 6, or 12 sided die? I ll input the test data (which is 12). text saying You rolled a 12 sided and the number the die landed on is followed by a random number between 1 and Test to see if the program correctly identifies an incorrect number of sides for a dice. 3 When asked What sided die do you want to roll, a 4, 6, or 12 sided die? I ll input the test data (which is 3). text saying I'll ask you again! What sided die do you want to roll, a 4, 6, or 12 sided die? leaving the user free to try again. 5 Test to see if the program correctly identifies incorrect value input in the die number section. hello When asked What sided die do you want to roll, a 4, 6, or 12 sided die? I ll input the test data (which is hello ). text saying I'll ask you again! What sided die do you want to roll, a 4, 6, or 12 sided die? that says You rolled a 12 sided die and the number that the die landed on is 2 which is just as predicted, with the ending 2 being a random number between 1 and 12. saying I'll ask you again! What sided die do you want to roll, a 4, 6, or 12 sided die? which is just as predicted as the user was then allowed to make another attempt. saying I'll ask you again! What sided die do you want to roll, a 4, 6, or 12 sided die? which is 10
11 leaving the user free to try again. 6 Test to see if the user can repeat the process as many times as required. yes When asked Would you like to continue? Yes or no! I ll input the test data (which is yes ). 7 Test to see if user can choose not to repeat the process so that they can quit the program. no When asked Would you like to continue? Yes or no! I ll input the test data (which is no ). 8 Test to see if the program correctly identifies incorrect input in the continue section. hello When asked Would you like to continue? Yes or no! I ll input the test data (which is hello ). text saying Yay and then ask the user What sided die do you want to roll, a 4, 6, or 12 sided die? text saying Ahh well! before it stops. text saying I'll ask you again! Would you like to continue? Yes or no! before the user has another attempt at giving an input. just as predicted as the user was then allowed to make another attempt. saying Yay and then What sided die do you want to roll, a 4, 6, or 12 sided die? which is just as predicted. saying Ahh well! before it stopped which is just as predicted. saying I'll ask you again! Would you like to continue? Yes or no! Evidence: 1. 11
12
13 Evaluation: I believe this code is successful because it follows all the success criteria that I explained in the Analysis. I said that: 13
14 would have the user able to input which of the dice is thrown. Which I ve done by having an integer variable called sidesint that is set to the integer conversion of the user s input when asked "What sided die do you want to roll, a 4, 6 or 12 sided die? must generate a random number between 1 and the user s input. Which I ve done by having if statements which are run depending on the value of sidesint and inside each of these if statements is an integer variable called result that s set to a random number between 1 and whatever the user s input happened to be. output the amount of sides on the die and the number it has landed on. Which I ve done by having the print function called and given the string input of You rolled a plus the number of sides on the dice plus and the number that the dice has landed on is plus the value of result. The user able to repeat the process as many times as they want. Which I ve done by creating a string variable that s set to the user s input when asked "Would you like to continue? Yes or no!" This input is then used to decide whether or not the value of continued is set to true or false, and if it is set to false, then the program will stop, but if it is set to true then the program will repeat the process. able to simulate a 4 sided die. Which I ve done by having it so that if the user inputs a 4 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?, an integer variable called result will be set to a random number between 1 and 4. The value of result will then be outputted along with the number of sides of dice which in this case is 4. able to simulate a 6 sided die. Which I ve done by having it so that if the user inputs a 6 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?, an integer variable called result will be set to a random number between 1 and 6. The value of result will then be outputted along with the number of sides of dice which in this case is 6. able to simulate a 12 sided die. Which I ve done by having it so that if the user inputs a 12 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?, an integer variable called result will be set to a random number between 1 and 12. The value of result will then be outputted along with the number of sides of dice which in this case is 12. able to identify incorrect die-number input. Which I ve done firstly by having an exception when setting the value of sidesint to the integer conversion of the user s input. So that if the user s input isn t in digits, the program will remain running rather than crash and will instead set the value of sidesint to 0. Secondly, I ve put a while statement that will continue to ask for the user s input until it is either 4, 6 or
15 My solution is good because it meets all the success criteria and efficiently deals with invalid user input. Also, it doesn t allow the user to continue until they ve given a valid response, while other programs would simply stop the process and simply assume that the user has decided to quit, which could be annoying if done accidentally. My solution is bad because it is very inefficient and doesn t use functions to increase efficiency and the ability to read and understand the code. Also, my program is bad because it doesn t allow the user to quit until the process is complete. The easiest part of the coding was figuring out how to get the program to repeat as many times as required as all this involved was creating a while loop that depended on a boolean variable having a value of true. The hardest part of the coding was deciding how to deal with characters being inputted by the user when they re supposed to enter digits. I solved this by finding out about the existence of exceptions and making use of them in this scenario. I could improve my solution by adding a function to roll the dice that takes a parameter for the amount of sides, rather than having a repeated section as I ve done. Also, I could add in a predetermined answer into the question that asks how many sides of dice that quits the program early. 15
Text Input and Conditionals
Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()
More informationGetting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners
Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008
More information(Python) Chapter 3: Repetition
(Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the
More informationThe Big Python Guide
The Big Python Guide Big Python Guide - Page 1 Contents Input, Output and Variables........ 3 Selection (if...then)......... 4 Iteration (for loops)......... 5 Iteration (while loops)........ 6 String
More informationHello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications
Hello World! Computer Programming for Kids and Other Beginners by Warren Sande and Carter Sande Chapter 1 Copyright 2009 Manning Publications brief contents Preface xiii Acknowledgments xix About this
More informationCS 115 Lecture 4. More Python; testing software. Neil Moore
CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement
More informationCHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS
CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations
More informationPython Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Objectives æ To understand the programming pattern simple decision and its implementation
More informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2014 Chapter 7 Part 2 The Department of Computer Science Quick review one-way or simple decision if : two-way decision
More informationComp 151. Control structures.
Comp 151 Control structures. admin quiz this week believe it or not only 2 weeks from exam. one a week each week after that. idle debugger Debugger: program that will let you look at the program as it
More informationComputer Science 217
Computer Science 17 Midterm Exam March 5, 014 Exam Number 1 First Name: Last Name: ID: Class Time (Circle One): 1:00pm :00pm Instructions: Neatly print your names and ID number in the spaces provided above.
More informationAn Introduction to Python for KS4!
An Introduction to Python for KS4 Python is a modern, typed language - quick to create programs and easily scalable from small, simple programs to those as complex as GoogleApps. IDLE is the editor that
More informationage = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s
Intro to Python Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences
More informationLecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming
Lecture Loops && Booleans Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda (In-)definite loops (for/while) Patterns: interactive loop and sentinel loop Solve problems using (possibly
More informationDecisions, Decisions. Testing, testing C H A P T E R 7
C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little
More informationHello, World! An Easy Intro to Python & Programming. Jack Rosenthal
An Easy Intro to Python & Programming Don t just buy a new video game, make one. Don t just download the latest app, help design it. Don t just play on your phone, program it. No one is born a computer
More informationLoop structures and booleans
Loop structures and booleans Michael Mandel Lecture 7 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture07final.ipynb
More informationCOMP 110 Programming Exercise: Simulation of the Game of Craps
COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,
More informationPython 2.7 will also work for this workshop due to certain language rules, however I prefer working with the most current stable version.
, webmaster@cse-club.com Fall 2014 In this workshop, we will be creating a basic text-based game to learn the basics Python programming language. By the end of this workshop, you will learn: The syntax
More informationComp 151. Control structures.
Comp 151 Control structures. admin For these slides read chapter 7 Yes out of order. Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed one after the other. While
More informationAn Introduction to Python
An Introduction to Python Day 2 Renaud Dessalles dessalles@ucla.edu Python s Data Structures - Lists * Lists can store lots of information. * The data doesn t have to all be the same type! (unlike many
More informationGirls Programming Network. Markov Chains Workbook 1
Girls Programming Network Markov Chains Workbook 1 This project was created by GPN Australia for GPN sites all around Australia! This workbook and related materials were created by tutors at: Sydney, Canberra
More informationKey Differences Between Python and Java
Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.
More informationNESTED IF STATEMENTS AND STRING/INTEGER CONVERSION
LESSON 15 NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION OBJECTIVE Learn to work with multiple criteria if statements in decision making programs as well as how to specify strings versus integers in
More informationPython Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Objectives n To understand the programming pattern simple decision and its implementation
More informationPython Programming: An Introduction To Computer Science
Python Programming: An Introduction To Computer Science Chapter 8 Loop Structures and Booleans Python Programming, 3/e 1 Objectives To understand the concepts of definite and indefinite loops as they are
More informationTry typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.
Name: Date: Instructions: PYTHON - INTRODUCTORY TASKS Open Idle (the program we will be using to write our Python codes). We can use the following code in Python to work out numeracy calculations. Try
More informationPython Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed
More informationThe while Loop 4/6/16 4
Chapter 4: Loops Chapter Goals To implement while and for loops To hand-trace the execution of a program To become familiar with common loop algorithms To understand nested loops To implement programs
More informationSt. Benedict s High School. Computing Science. Software Design & Development. (Part 1 Computer Programming) National 5
Computing Science Software Design & Development (Part 1 Computer Programming) National 5 VARIABLES & DATA TYPES Variables provide temporary storage for information that will be needed while a program is
More informationThe current topic: Python. Announcements. Python. Python
The current topic: Python Announcements! Introduction! reasons for studying languages! language classifications! simple syntax specification Object-oriented programming: Python Types and values Syntax
More informationPython Problems MTH 151. Texas A&M University. November 8, 2017
Python Problems MTH 151 Texas A&M University November 8, 2017 Introduction Hello! Welcome to the first problem set for MTH 151 Python. By this point, you should be acquainted with the idea of variables,
More informationLecture 02 Making Decisions: Conditional Execution
Lecture 02 Making Decisions: Conditional Execution 1 Flow of Control Flow of control = order in which statements are executed By default, a program's statements are executed sequentially, from top to bottom.
More informationLESSON 3 CONTROL STRUCTURES
LESSON CONTROL STRUCTURES PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Logic, Logical Operators, AND Relational Operators..... - Logical AND (&&) Truth
More informationHonors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F
PROGRAM 4A Full Names (25 points) Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F This program should ask the user for their full name: first name, a space, middle name, a space,
More informationBasic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2
Basic Concepts Computer Science Computer - Science - Programming history Algorithms Pseudo code 2013 Andrew Case 2 Basic Concepts Computer Science Computer a machine for performing calculations Science
More informationQuiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.
Python 1 Quiz All Code Clubs must be registered. Registered clubs appear on the map at codeclub.org.uk - if your club is not on the map then visit jumpto.cc/18cplpy to find out what to do. Introduction:
More informationCommon Loop Algorithms 9/21/16 42
Common Loop Algorithms 9/21/16 42 Common Loop Algorithms 1. Sum and Average Value 2. Coun4ng Matches 3. Promp4ng un4l a Match Is Found 4. Maximum and Minimum 5. Comparing Adjacent Values 9/21/16 43 Sum
More informationMITOCW watch?v=0jljzrnhwoi
MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To
More informationOutline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression
Outline ECS 10 10/8 Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit( ) Example: Coin flipping (if time permits) Announcements Professor Amenta
More information3 Nonlocal Exit. Quiz Program Revisited
3 Nonlocal Exit This chapter is about the commands catch and throw. These commands work together as a kind of super-stop command, which you can use to stop several levels of procedure invocation at once.
More informationBB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class:
KS3 Programming Workbook INTRODUCTION TO BB4W BBC BASIC for Windows Name: Class: Resource created by Lin White www.coinlea.co.uk This resource may be photocopied for educational purposes Introducing BBC
More informationCMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi
CMPT 120 Control Structures in Python Summer 2012 Instructor: Hassan Khosravi The If statement The most common way to make decisions in Python is by using the if statement. The if statement allows you
More informationIntroduction to Python (All the Basic Stuff)
Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions
More informationGetting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013
CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?
More informationCS 115 Lecture 8. Selection: the if statement. Neil Moore
CS 115 Lecture 8 Selection: the if statement Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 24 September 2015 Selection Sometime we want to execute
More informationSelection the If Statement Try Catch and Validation
Selection the If Statement Try Catch and Validation The main statement used in C# for making decisions depending on different conditions is called the If statement. A second useful structure in a similar
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 7 Part 2 Instructor: Long Ma The Department of Computer Science Quick review one-way or simple decision if :
More informationCS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?
1 CS 105 Lab 3 The purpose of this lab is to practice the techniques of making choices and looping. Before you begin, please be sure that you understand the following concepts that we went over in class:
More informationObjectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions
CSE1720 Click to edit Master Week text 01, styles Lecture 02 Second level Third level Fourth level Fifth level Winter 2015! Thursday, Jan 8, 2015 1 Objectives for this class meeting 1. Conduct review of
More informationIntroduction to the Java Basics: Control Flow Statements
Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared
More informationCONDITION CONTROLLED LOOPS. Introduction to Programming - Python
CONDITION CONTROLLED LOOPS Introduction to Programming - Python Generating Random Numbers Generating a random integer Sometimes you need your program to generate information that isn t available when you
More informationMaking Decisions In Python
Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action. Decision Making Is All About Choices My next vacation? Images:
More informationBasics of Programming with Python
Basics of Programming with Python A gentle guide to writing simple programs Robert Montante 1 Topics Part 3 Obtaining Python Interactive use Variables Programs in files Data types Decision-making Functions
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 8 Part 1 The Department of Computer Science Chapter 8 Loop Structures and Booleans 2 Objectives To understand the concepts
More informationBegin to code with Python Obtaining MTA qualification expanded notes
Begin to code with Python Obtaining MTA qualification expanded notes The Microsoft Certified Professional program lets you obtain recognition for your skills. Passing the exam 98-381, "Introduction to
More informationPart III Appendices 165
Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.
More information6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!
6.189 Project 1 Readings Get caught up on all the readings from this week! What to hand in Print out your hangman code and turn it in Monday, Jaunary 10 at 2:10 PM. Be sure to write your name and section
More informationAdministrativia. CS107 Introduction to Computer Science. Readings. Algorithms. Expressing algorithms
CS107 Introduction to Computer Science Lecture 2 An Introduction to Algorithms: and Conditionals Administrativia Lab access Searles 128: Mon-Friday 8-5pm (unless class in progress) and 6-10pm Sat, Sun
More informationProgramming with Python
Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2
More informationLOOPS. Repetition using the while statement
1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while
More informationScripting Languages. Python basics
Scripting Languages Python basics Interpreter Session: python Direct conversation with python (>>>) Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright",
More informationCreate a Login System in Visual Basic. Creating a login system. Start a new visual basic Windows Forms application project. Call it Login System
Creating a login system Start a new visual basic Windows Forms application project Call it Login System Change the form TITLE from Form1 to Login System Add the following to the form Component Text Name
More informationChapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)
Chapter 5 : Informatics practices Class XI ( As per CBSE Board) Conditional & Looping Constructs Control Statements Control statements are used to control the flow of execution depending upon the specified
More informationLecture 3. Input, Output and Data Types
Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and
More informationThe Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 2 Control 1 Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Control: A Quick Overview 2 Selection
More informationVisualize ComplexCities
Introduction to Python Chair of Information Architecture ETH Zürich February 22, 2013 First Steps Python Basics Conditionals Statements Loops User Input Functions Programming? Programming is the interaction
More informationJava Programming. Computer Science 112
Java Programming Computer Science 112 Yay Programming! How did the RPS program go? Did you notice the part where I pointed at the answer on the board that we did together? Biggest problems in NumbersAndMath
More informationLecture Transcript While and Do While Statements in C++
Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer
More informationCS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania
CS100: CPADS Decisions David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania James Moscola Decisions Just like a human, programs need to make decisions - Should turtle turn
More informationDiscussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Variables You have to instruct your computer every little thing it needs to do even
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program
More informationLECTURE 04 MAKING DECISIONS
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 04 MAKING DECISIONS
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 7 Part 1 The Department of Computer Science Objectives 2 To understand the programming pattern simple decision and its implementation
More informationFlow Control: Branches and loops
Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the
More informationChapter Goals. Contents LOOPS
CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common
More informationINTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0
INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0 OCTOBER 2014 Python Selection and Conditionals 1 SELECTION AND CONDITIONALS WHAT YOU MIGHT KNOW ALREADY You will probably be familiar
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to
More informationCS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore
CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:
More informationFlow Control. So Far: Writing simple statements that get executed one after another.
Flow Control So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer
More informationReview Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts
Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts Simple Data Types There are a number of data types that are considered primitive in that they contain only a single value. These data
More informationLecture 10: for, do, and switch
Lecture 10: for, do, and switch Jiajia Liu Recall the while Loop The while loop has the general form while ( boolean condition ) { The while loop is like a repeated if statement. It will repeat the statements
More information1 Lecture 6: Conditionals and Exceptions
L6 June 16, 2017 1 Lecture 6: Conditionals and Exceptions CSCI 1360E: Foundations for Informatics and Analytics 1.1 Overview and Objectives In this lecture, we ll go over how to make "decisions" over the
More informationENGR 101 Engineering Design Workshop
ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions CSE 142 - Computer Programming I 1 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >=
More informationPython - Week 3. Mohammad Shokoohi-Yekta
Python - Week 3 Mohammad Shokoohi-Yekta 1 Objective To solve mathematic problems by using the functions in the math module To represent and process strings and characters To use the + operator to concatenate
More informationPython Activity 5: Boolean Expressions and Selection Statements
Python Activity 5: Boolean Expressions and Selection Statements "True or False and making choices" Learning Objectives Students will be able to: Content: Explain the three types of programming structures
More informationint n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;
Debugging Some have said that any monkey can write a program the hard part is debugging it. While this is somewhat oversimplifying the difficult process of writing a program, it is sometimes more time
More informationIf Statements, For Loops, Functions
Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements
More informationReal Python: Python 3 Cheat Sheet
Real Python: Python 3 Cheat Sheet Numbers....................................... 3 Strings........................................ 5 Booleans....................................... 7 Lists.........................................
More informationDECISION STRUCTURES: USING IF STATEMENTS IN JAVA
DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you
More informationControl Structures 1 / 17
Control Structures 1 / 17 Structured Programming Any algorithm can be expressed by: Sequence - one statement after another Selection - conditional execution (not conditional jumping) Repetition - loops
More informationChapter 5 Errors. Bjarne Stroustrup
Chapter 5 Errors Bjarne Stroustrup www.stroustrup.com/programming Abstract When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications,
More informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking
More informationChapter 2 Writing Simple Programs
Chapter 2 Writing Simple Programs Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle (www.si182.com) Software Development Process Figure out the problem - for
More informationErrors. And How to Handle Them
Errors And How to Handle Them 1 GIGO There is a saying in computer science: Garbage in, garbage out. Is this true, or is it just an excuse for bad programming? Answer: Both. Here s what you want: Can you
More informationPart 1 Simple Arithmetic
California State University, Sacramento College of Engineering and Computer Science Computer Science 10A: Accelerated Introduction to Programming Logic Activity B Variables, Assignments, and More Computers
More information