Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 91 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 91 -

Image of the Page - 91 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python