ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 16 Sep 2016 03:05:19 +0200If , else, case?https://ask.sagemath.org/question/34830/if-else-case/Hallo!
Is there a function to help me out to test a condition without a 'cascade' of 'else(s)'? Like a "**case**"? I'm trying hard to find out a solution using the build-in help/manual and could not find a "case" statement. *Thank you in advance.*
r = randint(1,3)
if r == 1:
print 'One.'
if r == 2:
print 'Two.'
if r == 3:
print 'Three.'Fri, 16 Sep 2016 00:24:08 +0200https://ask.sagemath.org/question/34830/if-else-case/Answer by kcrisman for <p>Hallo!</p>
<p>Is there a function to help me out to test a condition without a 'cascade' of 'else(s)'? Like a "<strong>case</strong>"? I'm trying hard to find out a solution using the build-in help/manual and could not find a "case" statement. <em>Thank you in advance.</em></p>
<pre><code>r = randint(1,3)
if r == 1:
print 'One.'
if r == 2:
print 'Two.'
if r == 3:
print 'Three.'
</code></pre>
https://ask.sagemath.org/question/34830/if-else-case/?answer=34832#post-id-34832This is a Python question ... and, unsurprisingly, it has been asked before, though I don't think on this forum.
- [Python's tutorial](https://docs.python.org/3/tutorial/controlflow.html) suggests elif elif elif
- [This Stackoverflow question](http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) has a lot of people suggesting dictionaries that you check against
- There is even [an FAQ about this very thing](https://docs.python.org/3/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python) and a suggestion about [it was rejected](https://www.python.org/dev/peps/pep-3103/) nearly a decade ago.Fri, 16 Sep 2016 02:53:22 +0200https://ask.sagemath.org/question/34830/if-else-case/?answer=34832#post-id-34832Answer by tmonteil for <p>Hallo!</p>
<p>Is there a function to help me out to test a condition without a 'cascade' of 'else(s)'? Like a "<strong>case</strong>"? I'm trying hard to find out a solution using the build-in help/manual and could not find a "case" statement. <em>Thank you in advance.</em></p>
<pre><code>r = randint(1,3)
if r == 1:
print 'One.'
if r == 2:
print 'Two.'
if r == 3:
print 'Three.'
</code></pre>
https://ask.sagemath.org/question/34830/if-else-case/?answer=34833#post-id-34833Sage uses the Python programming language, and there is no `case` statement in Python, instead it is recommended to use `if elif elif ...`, see:
- https://docs.python.org/2.7/tutorial/controlflow.html
- https://www.python.org/dev/peps/pep-3103/ (rejected proposal)
Fri, 16 Sep 2016 02:58:19 +0200https://ask.sagemath.org/question/34830/if-else-case/?answer=34833#post-id-34833Comment by kcrisman for <p>Sage uses the Python programming language, and there is no <code>case</code> statement in Python, instead it is recommended to use <code>if elif elif ...</code>, see:</p>
<ul>
<li><a href="https://docs.python.org/2.7/tutorial/controlflow.html">https://docs.python.org/2.7/tutorial/...</a></li>
<li><a href="https://www.python.org/dev/peps/pep-3103/">https://www.python.org/dev/peps/pep-3...</a> (rejected proposal)</li>
</ul>
https://ask.sagemath.org/question/34830/if-else-case/?comment=34835#post-id-34835Haha, we nearly had the same answer!Fri, 16 Sep 2016 03:02:03 +0200https://ask.sagemath.org/question/34830/if-else-case/?comment=34835#post-id-34835Comment by tmonteil for <p>Sage uses the Python programming language, and there is no <code>case</code> statement in Python, instead it is recommended to use <code>if elif elif ...</code>, see:</p>
<ul>
<li><a href="https://docs.python.org/2.7/tutorial/controlflow.html">https://docs.python.org/2.7/tutorial/...</a></li>
<li><a href="https://www.python.org/dev/peps/pep-3103/">https://www.python.org/dev/peps/pep-3...</a> (rejected proposal)</li>
</ul>
https://ask.sagemath.org/question/34830/if-else-case/?comment=34836#post-id-34836And i was just about to open a ticket for the chromatic number of the empty graph, when i saw your ticket !!!Fri, 16 Sep 2016 03:05:19 +0200https://ask.sagemath.org/question/34830/if-else-case/?comment=34836#post-id-34836