-1
def get8bit():
    while True:
        try:
            bNum = int(input('Please enter your 8 bit number'))
        except:
            print('Please only enter numbers 0 and 1')
        return bNum

def valid8bit(x):
    validChoices = [00000000,11111111]
    while x not in validChoices:
        x = int(input('Please only enter an 8 bit number:'))
        if x == validChoices:
         break
    return x

def convertodeci(y):
    y = y ** 2
    return y
    
user = get8bit()
thisValid = valid8bit(user)
userTodeci = convertodeci(thisValid)
print(userTodeci)

I am trying to write a function that both validates that the number the user entered is 0 or 1 AND is 8 bits in length. I know I need to keep looping until it is correct but I cannot get it to validate the input correctly. Please show me what I'm doing wrong! I want to learn :)

Greg
  • 19
  • 4
  • 1
    What is an input for which the program gives wrong results? – mkrieger1 May 17 '21 at 01:24
  • 2
    The code you've shown isn't indented correctly, so it can't run at all and it can't be the code that gives you right or wrong results. – mkrieger1 May 17 '21 at 01:25
  • an invalid input would be anything other than 8 0's or 8 1's so if they enter 00000020 it would be invalid – Greg May 17 '21 at 01:26
  • Does this answer your question? [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – mkrieger1 May 17 '21 at 01:26
  • 1
    What is the result of your program when they enter 00000020? – mkrieger1 May 17 '21 at 01:26
  • 1) You can't check if some value is an integer by doing `x != int`. 2) You're converting `x` to `int` and then checking its length. An integer does not have a length, but a string does. – enzo May 17 '21 at 01:32
  • Hello! I updated the post to fix the formatting errors and give a better over view of the whole system. This just doesn't work because the list choices only allows the user to have either a full byte of 0 or full byte of 1 – Greg May 17 '21 at 01:39

1 Answers1

1

Your code contains a lot of complications and mistakes. For one, you want to accept a string like '00000001', but that's not a valid integer representation, so your first test would make it fail, if it didn't contain errors itself.

You then have a user input command in the middle of a function that supposedly checks if a given number is a valid 8-bit integer, which seems like bad design and not part of the problem you're solving.

And finally, you only try to check if what was entered is 8 1's or 8 0's, but that's not what you want to do either. And the check won't work since 00000000 is not a valid string literal (nor a valid integer).

You were probably after something like:

def valid8bit(s):
    return len(s) == 8 and all(ch in '01' for ch in s)

x = input('Enter an 8 digit binary number:')
print(valid8bit(x))

There's many ways to skip a cat, as @Enzo suggested, this is another approach:

def valid8bit(s):
    try:
        return int(s, 2) in range(256)
    except ValueError:
        return False
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Many ways to get there, I went something a bit more literal, but of course yours works just fine and is preferable in practice @enzo, given a try except around it – Grismar May 17 '21 at 01:36
  • @mkrieger1 You're correct, I'm so dumb. – enzo May 17 '21 at 01:37
  • Thank you guys for pointing these errors out to me! I'm a first year student so I am really appreciative for the feedback I want to get better! I totally see where I made these errors now. I think I totally need to spend more time thinking about how the functions should work rather than coding them off the jump and being frustrated that they dont work – Greg May 17 '21 at 01:42
  • @Greg being frustrated that code doesn't work is part of being a programmer, it's not so bad to get used to it early. – Mark Ransom May 17 '21 at 01:51
  • @MarkRansom is correct that frustration goes with the territory, but your response is a good one, I think. Ask yourself, why am I putting this here, what problem am I trying to solve? Is there another solution that doesn't have this problem? But most importantly, what do I really want to do here and what's the most basic way to break it down (once you understand all of the basic language constructs of the language of choice - and Python is a good choice). – Grismar May 17 '21 at 03:28