{"id":5258,"date":"2017-03-24T18:39:46","date_gmt":"2017-03-24T17:39:46","guid":{"rendered":"http:\/\/www.valcon.it\/python\/?p=5258"},"modified":"2024-01-19T18:43:06","modified_gmt":"2024-01-19T17:43:06","slug":"leggere-un-numero","status":"publish","type":"post","link":"https:\/\/www.valcon.it\/python\/leggere-un-numero\/","title":{"rendered":"Leggere un numero"},"content":{"rendered":"\n<p>La semplice lettura di un numero da tastiera<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumero = int(input(&quot;Numero&quot;))\n<\/pre><\/div>\n\n\n<p>crea spesso problemi se il numero deve appartenere a un certo intervallo di valori.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Soluzione 1<\/h2>\n\n\n\n<p>Il ciclo <strong>while<\/strong> ripete la lettura se il numero non appartiene all&#8217;intervallo [<strong>inf<\/strong>, <strong>sup<\/strong>]<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninf = 1 \nsup = 10 \nnumero = inf-1 \nwhile(numero &lt; inf) or (numero &gt; sup): \n    numero = int(input(&quot;Numero: &quot;))\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Soluzione 2<\/h2>\n\n\n\n<p>Come funzione<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef leggiIntero(inf, sup):\n    n=inf-1\n    while(n &lt; inf) or (n &gt; sup):\n        n=int(input(&quot;Numero: &quot;))\n    return n\n\nnumero=leggiIntero(1,10)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Soluzione 3<\/h2>\n\n\n\n<p>Con un messaggio specifico<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef leggiIntero(inf, sup): \n    msg=&quot;Numero &#x5B;&quot; + str(inf) + &quot;,&quot; + str(sup)+&quot;] = &quot; \n    n=inf-1 \n    while(n &lt; inf) or (n &gt; sup): \n        n=int(input(msg)) \n    return n\n\nnumero=leggiIntero(1,10)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Soluzione 4<\/h2>\n\n\n\n<p>La stringa digitata dall&#8217;utente potrebbe non essere un intero (float).<\/p>\n\n\n\n<p>Il ciclo <strong>while<\/strong> e la struttura <strong>try<\/strong> &#8230; <strong>except<\/strong> &#8230; permettono di<\/p>\n\n\n\n<ol>\n<li><em>continuare <\/em>(<strong>continue<\/strong>) con il tentativo di input in caso di errore nella conversione<\/li>\n\n\n\n<li><em>terminare<\/em> (<strong>break<\/strong>) in caso di successo.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwhile(True): \n    try: \n        numero=int(input(&quot;Numero: &quot;)) \n        break \n    except: \n        continue\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>La semplice lettura di un numero da tastiera crea spesso problemi se il numero deve appartenere a un certo intervallo di valori. Soluzione 1 Il ciclo while ripete la lettura se il numero non appartiene all&#8217;intervallo [inf, sup] Soluzione 2 Come funzione Soluzione 3 Con un messaggio specifico Soluzione 4 La stringa digitata dall&#8217;utente potrebbe &#8230; <a title=\"Leggere un numero\" class=\"read-more\" href=\"https:\/\/www.valcon.it\/python\/leggere-un-numero\/\" aria-label=\"Leggi di pi\u00f9 su Leggere un numero\">Leggi tutto<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"_links":{"self":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5258"}],"collection":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/comments?post=5258"}],"version-history":[{"count":30,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5258\/revisions"}],"predecessor-version":[{"id":23589,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5258\/revisions\/23589"}],"wp:attachment":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/media?parent=5258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/categories?post=5258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/tags?post=5258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}