Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 91 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 91 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 91 -

Bild der Seite - 91 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 91 -

4.2 ProgrammingasaStep-WiseStrategy 91 return 1 without actually testing anything. In this way, we will be able to run the program,while still havingunfinishedparts in there. We have introducedN as a variable here to allow easy adjustment of “problem size” (the total number of questions will beN*N). We knowNmust be 10, but that requirementapplies to the finalversiononly.Thus,we are free to doour steps with a smallerN, and that makes life much easier for us when assessing code behavior. If you have not already done so, go through the code by hand to confirm that you understandwhathappens, in whatorder. Whenexecuted, theprogramsimplyprints: *** Welcome to the times tables test! *** (To stop: ctrl-c) 1*1 = 1*1 = 1 Your score is now: 1 1*2 = 1*2 = 2 Your score is now: 2 2*1 = 2*1 = 2 Your score is now: 3 2*2 = 2*2 = 4 Your score is now: 4 Finished! Your final score: 4 (max: 4) From the printout,we see that the two functionsseem to get the right arguments in eachcall.We are thus readyfor thenextstep, i.e., to implement the functionbodies of the twofunctions. 4.2.3 The2ndVersionofOurCode After implementing the remaining parts of the code, we have a version (times_tables_2.py) of our program that actually does the testing that was asked for.Thecode reads def ask_user(a, b): """get answer from user: a*b = ?""" question = ’{:d} * {:d} = ’.format(a, b) answer = int(input(question)) return answer def points(a, b, answer_given): """Check answer. Correct: 1 point, else 0""" true_answer = a*b if answer_given == true_answer: print(’Correct!’) return 1 else: print(’Sorry! Correct answer was: {:d}’.format(true_answer)) return 0
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python