Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's better to use a <code>while</code>-loop to control the repetition, rather than a <code>for</code>-loop. This way the users aren't limited to a fixed number of repeats, <em>they can continue as long as they want</em>. In order to quit, users enter a value &lt;= 0.</p> <pre><code>from __future__ import division import math function = int(raw_input("Type function no.: ")) while function &gt; 0: if function == 1: a = float(raw_input ("Enter average speed: ")) b = float(raw_input ("Enter length of path: ")) answer = b/a print "Answer=", float(answer), elif function == 2: mass_kg = int(input("What is your mass in kilograms?" )) mass_stone = mass_kg * 2.2 / 14 print "You weigh", mass_stone, "stone." print 'Enter a value &lt;= 0 for function number to quit.' function = int(raw_input("Type function no.: ")) </code></pre> <p>You can tweak this (e.g., the termination condition) as needed. For instance you could specify that 0 be the only termination value etc.</p> <p><em><strong>An alternative</em></strong> is a loop that runs "forever", and break if a specific function number is provided (in this example <code>0</code>). Here's a skeleton/sketch of this approach:</p> <pre><code> function = int(raw_input("Type function no.: ")) while True: if function == 1: ... elif function == 2: ... elif function == 0: break # terminate the loop. print 'Enter 0 for function number to quit.' function = int(raw_input("Type function no.: ")) </code></pre> <p><em>Note</em>: A <code>for</code>-loop is most appropriate if you are <strong>iterating a known/fixed number of times</strong>, for instance over a sequence (like a list), or if you want to limit the repeats in some way. In order to give your users more flexibility a <code>while</code>-loop is a better approach here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload