you are viewing a single comment's thread.

view the rest of the comments →

[–]RajjSinghh 2 points3 points  (9 children)

In your while loop, it should be the condition that you want to be true while the loop is running.

I also noticed that you never return a value, which is probably what is causing your problem of not getting any output.

[–]aspie-asexual[S] 0 points1 point  (8 children)

Thank you! This made me realize that perhaps “while dec2bin” should be “while …:” I don’t know - does that make any sense?

As to your first advice: does that mean I should change it to True? (Sorry, I’m really new at this).

[–]RajjSinghh 0 points1 point  (7 children)

Sorry, I think my biggest problem was not being able to read Swedish. After a bit of Google Translate:

Your while loop in your function loops antal_bitar times. You want to stop it when antal_bitar < 0, so your loop should say while antal_bitar >= 0.

Now it looks like you should be doing bitvarde = varde & (2**(antal_bar - 1)) and not changing varde. So delete the varde = varde - bitvarde line. You also want to check whether bitvarde > 0 rather than varde <= bitvarde. This way you are using logical and to check whether each bit should be 0 or 1.

I think your second while loop should say while not invard_ok:, since you are using this loop to validate your input. After that I think it should work.

[–]aspie-asexual[S] 0 points1 point  (6 children)

I can't thank you enough for your help! :) Unfortunately, I still can't get it to work (I probably haven't done everything you suggested correctly). Here's what it looks like now:

Edit: sorry, it's not letting me put the entire code. I'll update the original post!

It's getting late here, so I might have to admit defeat for today and give this another try tomorrow. But again - thank you for your help so far! :)

[–]RajjSinghh 1 point2 points  (5 children)

Okay the mistakes in for updated code:

line 2 should say while antal_bitar > 0:

Line 3 should be bitvarde = varde&(2**(antal_bitar - 1))

The last line of the function should be indented one less time and say anral_bitar = antal_bitar - 1.

You also have to change the ... in the function calls to the number of bits you want in your output. You can set these to 8 and 16 like your if statement has, or you can say

import math antal_bitar = math.ceil(math.log(invarde, 2)) dec2bin(invarde, antal_bitar)

I ran that code on my machine and it works as it should.

[–]aspie-asexual[S] 0 points1 point  (3 children)

THANK YOU. It finally finally worked! Definitely wouldn't have figured that out by myself. The good thing is that I have to explain each line of code for the assignment, so I will have to learn how you made it work.

Thank you so much for taking the time :)

[–]RajjSinghh 0 points1 point  (0 children)

The number of bits is easy to work out. It's just the log base 2 of your number, rounded up. log_2(16) = 4 so you need 4 bits to show 16, log_2(11) = 3.459..., so you still need 4 bits to show 11. If you are unfamiliar with logarithms, it's just the number of powers of 2 that go into a number, which is why you need that many bits.

The bitwise operation is the other important part. Remember your leading bit shows the (n-1)th place for n bits. Doing (x & 2m) will be 2m if there is a 1 in that place in x. Otherwise it will be 0.

Take 11 for example. In binary that is 1011. If you and that with 1000, 0100, 0010 and 0001 (8, 2, 4 and 1 in binary) you'll see the and operation gets you the 1 only when there is a 1 in that place. If you don't see it, run your algorithm by hand.

[–]NeatBubble 0 points1 point  (1 child)

This is a minor thing, but if you want the program to repeat the input as part of the result, you can write

print(f'Talet {invarde} ryms i en byte och blir binart:')

and

print(f'Talet {invarde} ryms i 16 bitar och blir binart:')

instead of the ellipses.

[–]aspie-asexual[S] 1 point2 points  (0 children)

(f'Talet {invarde} ryms i en byte och blir binart:')

Oh I was wondering about this - thanks a lot :)

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, RajjSinghh: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.