程序代写 Interactive Program

Interactive Program
Credit: https://www.mathsisfun.com/algebra/linear-equations.html
The University of 1

Copyright By PowCoder代写 加微信 powcoder

Interactive Programs
– Programsareusuallyinteractive. input
Process (“transformation”)
The University of 2

Activity: Sandwich Maker
– Whatistheoutputoftheprogramsandwich.py?
– Ifyouruntheprogram5times,wouldtheoutputchange?
– Code readability. What do you think?
– Ed Lesson
The University of 3

Recap:print() function
– print() function allows concatenation of different pieces of information
with a single space as separation (keyword argument – sep).
Source: https://docs.python.org/3/library/functions.html#print
The University of 4

Interactive Programs
– Reality:Ourprogramssofar… print()
Process (“transformation”)
– We need a way to get the input from the user!
– Simplestform:Textfromstandardinput(keyboard)
The University of 5

Interactive Programs
– Programsareusuallyinteractive. input()
Process (“transformation”)
– Theinput()functionreadsalinefromstandardinput (keyboard) converts it to a string stripping the newline character.
The University of 6

input() function
Source: https://docs.python.org/3/library/functions.html#input
• Example in documentation uses an assignment statement: • Variable name: ???
• Value: ???
• Data type: ???
The University of 7

Activity: Echo – Can you hear me?
– Goto: https://edstem.org/au/courses/7472/lessons/18856/slides/134992
– Runtheprogramdemo.py.
– Type something in the terminal. Press Enter key once you are
– Whatdoyousee?
The University of 8

What did we just do?
– The computer did exactly as you told it to do! input()
– Theinput()functionreadsalinefromstandardinput (keyboard) converts it to a string stripping the newline character.
– To view the data entered by the user, use print()to output to the terminal.
The University of 9

Activity: Echo – Can you hear me?
– Goto: https://edstem.org/au/courses/7472/lessons/18856/slides/134992
– DoPart2:Echo – Hint:
– Theoutputofinput()istheinputofprint().
The University of 10

Interactive Programs in Reality
– Datareceivedfromtheuser,outputoftheinput()function,is usually processed e.g. compute, transform.
Process (“transformation”)
– Dataisstoredintovariablesforeasierretrievaland manipulation.
The University of 11

Activity: Echo – Can you hear me?
– Goto: https://edstem.org/au/courses/7472/lessons/18856/slides/134992
– DoPart3:Echowithlowervolume – Hint:
– Stringobjectshavemanymethodsincludingstr.upper()and str.lower()
– TesttheseinthePythonShell:
• “help”.upper()
• “HELP”.lower()
• input().swapcase()
The University of 12

Output Formatting
– Many ways to present the output of a program in a more readable format.
– Twoapproaches:
1. FormattedStringLiterals(f-strings)
• Prefixing the string with f or F
• Expression included inside string as {expression}
2. String format()method
• Objects passed into the str.format()replace items in brackets.
Your task:
• Run demo.py in Ed Lesson.
• Can you spot both approaches?
The University of 13

Output Formatting
– Twoapproaches:
1. FormattedStringLiterals(f-strings)
• Prefixing the string with f or F
• Expression included inside string as {expression}
• Example: Line 12
String format()method
• Objects passed into the str.format()replace items in brackets. • Example:Line13
Documentation: https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals The University of 14

Format Specification
– Specify presentation format within placeholder.
– Example:
– Widthof3andpadwithzerose.g.001 – print(f”{quantity:03d}”)
– Actualvalueisnotmodifiedduringformatting. – Documentation:
https://docs.python.org/3/library/string.html#formatspec
The University of 15

Activity: Sandwich Maker 2.0
– Writeaprogramthatpromptsthecustomerfortheirname, sandwich type and quantity. The program will always serve 2 extra sandwiches with their order.
– The program must produce the exact output as specified:
• Customer name is always capital case e.g. John, Sue, Naz.
• Sandwich type must always be upper case.
• The program confirms the number of sandwich by displaying all details to the terminal, see Sample Program Output.
– EdLesson
The University of 16

– Aprogramusuallyprocessesdata,e.g.canbefromstandard input using input().
– Variables are used to associate a value to an identifier i.e. variable name.
– Differentwaystoperformoutputformattingusing str.format()and f-string approach.
– ManystringmethodsavailableinPython.
The University of 17

Self-Study
– Chapter5–KeyboardInput.Downey,A.B.(2015).Think Python: How to Think Like a Computer Scientist (2e ed.). O’ , Incorporated.
– Chapter2.Downey,A.B.(2015).ThinkPython:HowtoThink Like a Computer Scientist (2e ed.). O’ , Incorporated.
The University of 18

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com