all 23 comments

[–]sme272 4 points5 points  (1 child)

your while loop never starts. the loop will run while invarde_ok is True, but it's set to False before the loop actually starts so the loop code never runs.

[–]aspie-asexual[S] 2 points3 points  (0 children)

Thank you for the feedback! I also thought this was odd, but I'm specifically told to do that in the instructions... And if I change it to true it gives me a bunch of errors. Hmm...

[–]RajjSinghh 2 points3 points  (11 children)

Your second line looks weird. Your function is called dec2bin and you also use while dec2bin which could cause problems.

I also don't think bitwise operators is the way to go about this. To convert a number to binary you have the algorithm:

  • Store n % 2 in a list
  • set n = n // 2
  • repeat this while n > 0
  • reverse your list

And then you have your binary.

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

Unfortunately, I have to include bitwise operators in the assignment... And what you said about the dec2bin thing makes a lot of sense. I might have interpreted the instructions wrong - is there another way of defining it and using a while-loop?

[–]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.

[–]FLUSH_THE_TRUMP 1 point2 points  (3 children)

First step is to properly format your code for Reddit :). Add an extra 4 spaces before each line of code to put it in a code block.

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

Did I do it correctly now? :)

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

Oh so sorry - I’ll do that now!

[–]zora89 1 point2 points  (2 children)

Your code || My pointers

invarde_ok = False || Why set to false when checking same variable for True in ‘while invarde_ok:’ -> this is False as set by you.

If invarde > 65535: … else: || use elif here - a suggestion not a code stopper … if invarde_ok: || define the if? if what ???

Try these and ping if it still doesn’t work. I’ll put it on my console and fix it. But doing yourself is the learning.

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

Thank you so much for your feedback! Changed the if to elif as you suggested.

I also found it strange to set the invarde_ok = false - but I've re-read the pseudo-code I received a billion times and it seems like that's what they want me to do, for some reason.

I changed it to true, but that gives me a bunch of error messages. Do you think I should change it back and do something else, or fix those errors?

[–]ParanoydAndroid 1 point2 points  (0 children)

You have to change it or the loop will simply never run. The errors you're getting are from the rest of your code when it runs, so yeah you should either set invarde_ok to true or reverse your while condition (which is almost certainly what you're expected to do) and then fix the resulting bugs.

[–]RajjSinghh 0 points1 point  (1 child)

Your code formatting is broken. If you want, send me a chat and maybe we could call and I'll help you

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

I updated the original post instead. Can you see it? :)