Note that there are some explanatory texts on larger screens.

plurals
  1. POif/elif mash up
    primarykey
    data
    text
    <p>This is my second question on this site and both of them deal with 'if' statements not working as expected. I've been learning Python for about 3-4 months and I remember back at the beginning when i was learning 'for', 'if's and 'while's I remember thinking that the 'if' statement is the simplest of the three; I'm starting to come to the realization that 'if' statements aren't as clear cut as I once thought.</p> <p>I'm writing a converter that converts between binary, decimal and hex without using any Python built-in math functions. I am using tkinter.</p> <p>The problem I will ask about deals with when the convert from 'binary' radio button is selected. below the 3 'convert from' radio buttons are 3 'convert to' check boxes. The following code is meant to convert from binary to decimal if the dec_bttn checkbox is checked and to hex if the hex_bttn checkbox is selected:</p> <pre><code>def from_binary(self, dec_bttn, hex_bttn): """ Performs conversion from base2 to base10 and base16. """ bits = '1010' #actual code used -&gt; self.input_str.get() exp = len(bits) -1 #actual code used -&gt; len(self.input_str.get()) - 1 # operate on valid string # converts to decimal if self.dec_bttn: dtot = 0 while exp &gt;= 1: for i in bits[:-1]: if i == "1": dtot += 2**exp elif i == "0": dtot = dtot exp -= 1 if bits[-1] == "1": dtot += 1 self.output_disp.delete(0.0, END) self.output_disp.insert(0.0, dtot) # convert to hex elif self.hex_bttn: hex_digits = { 10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f' } string_length = len(bits) exp = len(bits) - 1 if string_length &lt;= 4: htot = 0 while exp &gt;= 1: for i in bits[:-1]: if i == "1": htot += 2**exp elif i == "0": htot = htot exp -= 1 if bits[-1] == "1": htot += 1 for i in hex_digits.keys(): if i == htot: htot = hex_digits[i] else: htot = htot self.output_disp.delete(0.0, END) self.output_disp.insert(0.0, htot) </code></pre> <p>I have posted the entire 'from_binary' method. This is probably slightly more than needed but alittle too much is better than neglecting any amount.</p> <p>Because I have to eventually break the string up into half byte strings for the hex conversion it is set up now as &lt;= 4. So I am only testing it using 4 bits strings.</p> <p>As it stands right now, using 'elif self.hex_bttn' it converts binary to decimal regardless of which checkbox is selected. If I change that to 'if self.hex_bttn' then it converts to hex regardless of which checkbox is selected.</p> <p>The math and everything works right and outputs correctly. Its just the mix up with these 'if' statements that is holding up my progress. To my beginners mind it seems like a fairly straightforward test, but I am obviously missing something.</p> <p>I would greatly appreciate if someone could put me on the right path.</p> <p>Blessings F.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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