#πŸ”’ not sure where my codes going wrong

123 messages Β· Page 1 of 1 (latest)

shadow turret
#
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")


def correct_length(word):
    lower = word.lower()
    if len(word) >= 2 and len(word) <= 6:
        return True

def correct_type(word):
    x = 0
    while x < len(word):
        if word[x] not in letters and word[x] not in numbers:
            return False
        x += 1
    else:
        return True

def first_two(word):
    lower = word.lower()
    if lower[0] in letters and lower[1] in letters:
        return True

def no_numbers_middle(word):
    for i in word:
        if word[i] in numbers and word[i+1] in letters:
            return False
        else:
            return True

def cant_start_with_0(word):
    stringg = ""
    for i in word:
        if word[i] in numbers:
            stringg = stringg + word[i]

    if stringg[0] == "0":
        return False
    else:
        return True
def is_valid(word):
    if first_two(word) == True and correct_type(word) == True and correct_length(word) == True and no_numbers_middle(word) == True and cant_start_with_0(word) == True:
        return True

main()

haughty bisonBOT
#

@shadow turret

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

elfin ether
#

could you format your code like this please

shadow turret
#

yes wait sorry

#

ill send the original question aswell

elfin ether
#

just put the "py" on the same line as the ticks then it will have the colours as well

shadow turret
elfin ether
#

Are you getting an error or is it just not accepting your answer?

shadow turret
#

no the code works but it says invalid every time

#

even for inputs that should be valid

shadow turret
elfin ether
#

like this

shadow turret
elfin ether
#

i'm not sure

#

okay it doesn't matter

kindred copper
#

py wasn't on the same line as the `'s

elfin ether
#
def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")

so you're saying this is printing "invalid" no matter what

shadow turret
#

yeah no matter what input it gets it prints invalid

#

even for plates that should work

elfin ether
#

what input are you giving it

shadow turret
#

CS50

elfin ether
#

okay one min

shadow turret
#

thanks πŸ™‚

elfin ether
#

!e

letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

def main():
    plate = "CS50"
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")


def correct_length(word):
    lower = word.lower()
    if len(word) >= 2 and len(word) <= 6:
        return True

def correct_type(word):
    x = 0
    while x < len(word):
        if word[x] not in letters and word[x] not in numbers:
            return False
        x += 1
    else:
        return True

def first_two(word):
    lower = word.lower()
    if lower[0] in letters and lower[1] in letters:
        return True

def no_numbers_middle(word):
    for i in word:
        if word[i] in numbers and word[i+1] in letters:
            return False
        else:
            return True

def cant_start_with_0(word):
    stringg = ""
    for i in word:
        if word[i] in numbers:
            stringg = stringg + word[i]

    if stringg[0] == "0":
        return False
    else:
        return True
def is_valid(word):
    print(first_two(word) is True)
    print(correct_type(word) is True)
    print(correct_length(word) is True)
    print(no_numbers_middle(word) is True)
    print(cant_start_with_0(word) is True)
main()
haughty bisonBOT
# elfin ether !e ```py letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", ...

:x: Your 3.14 eval job has completed with return code 1.

001 | True
002 | False
003 | True
004 | Traceback (most recent call last):
005 |   File "/home/main.py", line 54, in <module>
006 |     main()
007 |     ~~~~^^
008 |   File "/home/main.py", line 6, in main
009 |     if is_valid(plate):
010 |        ~~~~~~~~^^^^^^^
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/KXLURILJXHKJYZLTIC7NZJUU4U

elfin ether
#

hm what've i done here

#

!e

def no_numbers_middle(word):
    for i in word:
        if word[i] in numbers and word[i+1] in letters:
            return False
        else:
            return True

print(no_numbers_middle("CS50"))
haughty bisonBOT
# elfin ether !e ```py def no_numbers_middle(word): for i in word: if word[i] in n...

:x: Your 3.14 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 8, in <module>
003 |     print(no_numbers_middle("CS50"))
004 |           ~~~~~~~~~~~~~~~~~^^^^^^^^
005 |   File "/home/main.py", line 3, in no_numbers_middle
006 |     if word[i] in numbers and word[i+1] in letters:
007 |        ~~~~^^^
008 | TypeError: string indices must be integers, not 'str'
elfin ether
#

You've got an error in this function

#

not sure why it wasn't erroring earlier

#

I think what you really wanted there was :

for i in len(word):
    if word[i] in numbers and word[i+1] in letters:
        ...
#

rather than
for i in word

#

because in your code i is a string

#

in my code i is an integer

#

@shadow turretthat make any sense?

shadow turret
#

like if i do i in word it becomes a string and then the next line im using it as an integer so it goes weird

#

i see i see

#

thanks thanks

elfin ether
#

also take note that aside from the error, print(correct_type(word) is True) is actually printing as false

#

so i suspect that function is incorrect

#

so take a look at it

shadow turret
#

yeah it’s still not working so ur right

velvet hollow
lunar owl
#

Solved?

shadow turret
# lunar owl Solved?

nah I tried to fix it and just got more errors LOL so I’ll just try tmr morning

shadow turret
random arch
shadow turret
#

ahah

random arch
#

If I get a solution I'll let u know

shadow turret
#

if u figure out let me know please

#

thank thanks

lunar owl
#

idk some stuff like is_valid lemme check that out and I will try this one too

slim rover
#

Unrelated to solving the problem but considering using string.ascii_letters and string.digits and whenever you implement something general that you think should've been done before it's probably done by a library. (You'll also not need to do .lower everywhere)

slim rover
lunar owl
#

punctuation_marks = [
".", # period
",", # comma
"?", # question mark
"!", # exclamation mark
":", # colon
";", # semicolon
"'", # apostrophe
""", # quotation mark
"-", # hyphen
"–", # en dash
"β€”", # em dash
"(", ")", # parentheses
"[", "]", # brackets
"{", "}", # braces
"...", # ellipsis
"/", # slash
]

haughty bisonBOT
#

Hey @lunar owl!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
elfin ether
#

it saves you typing them all out and placing in a list

#

!d string.ascii_letters

haughty bisonBOT
#

string.ascii_letters```
The concatenation of the [`ascii_lowercase`](https://docs.python.org/3/library/string.html#string.ascii_lowercase) and [`ascii_uppercase`](https://docs.python.org/3/library/string.html#string.ascii_uppercase) constants described below. This value is not locale-dependent.
shadow turret
#

oh I see

elfin ether
#

!e

import string

print("a" in string.ascii_letters)
haughty bisonBOT
slim rover
shadow turret
#

I haven’t done anything with importing external libraries but ill def keep it in mind

elfin ether
#

Its built-in so you don't have to do any pip installing or anything. Just import and you're ready to go

slim rover
shadow turret
#

😭 ok ok

#

I’ll go take a look pythons standard libraries then

slim rover
velvet hollow
#

One doesn't learn English by memorizing a dictionary, either.

#

But also, don't just "search" traditionally; look around. REPL tools like help and dir are your friends.

slim rover
random arch
# shadow turret 😭 ok ok

I finished my code, it looks like this
!e

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")


def is_valid(s):
    validPoints = 0
    numberPosition = 0
    if int(len(s)) >=2 and int(len(s)) <=6:
        validPoints +=1
    else:
        return False
    if s[0:2].isalpha():
        validPoints += 1
    for char in s:
        numberPosition +=1
        if char.isalpha() == False and char.isdigit() == False:
            return False
        if char.isdigit():
            if char != "0":
                validPoints +=1
                break
            else:
                break

        if any(char.isdigit() for char in s):
            if s[int(numberPosition):].isdigit():
                validPoints+=1
        else:
            validPoints+=2
    if validPoints >= 4:
        return True
    
main()

lunar owl
#

!paste

haughty bisonBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

lunar owl
#

took me around 2 hours oof but that was a fun problem

#

learned strings module

#

bring more of these everyday bubbles βœ…

random arch
#

Some of them are very complex, it's so good to learn, I keep check the docs of python

random arch
random arch
lunar owl
random arch
# lunar owl Didn't understand what u meant

I mean like, the problems of this course to solve, they're so good to learn, because to make it you need to keep trying to find new things that will help you to solve the problem

velvet hollow
# slim rover How? I wanna know I've always searched traditionally
>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_sentinel_dict', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>> type(string.capwords)
<class 'function'>
>>> help(string.capwords)
# shows what it does and how to use it
haughty bisonBOT
#

Hey @velvet hollow!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
lunar owl
#

I always used print(help())
And read the docs for it smt like that

random arch
lunar owl
#

I checked my code with some pros and they told me the loop logic could be better

#
return True``` was better 

Ic that was possible forgot "in" can be used in if statements.
#

So that changes a lot of things

slim rover
lunar owl
lunar owl
lunar owl
slim rover
#
def is_nopunc(plate):
    """Checks if the name plate obeys Rule#4"""
    for s in plate:
        for i in punctuation:
            if s == i:
                return False
            else:
                return True```
Try checking ASD.
This will fail.
Because it is actually checking it only for the first s and the first i. So it will always return True.
lunar owl
#

Oooooooo insane loop hole

#

Wow

slim rover
#

Whenever you're using else in a for loop and returning, It only checks the first value and returns

lunar owl
#

U meant ASD.

slim rover
#
def number(plate):
        """Checks if it is a number"""
        for i in plate:
            if i in digits:
                return True
            else:
                return False```
This is again the same issue
lunar owl
#

Ohh u meant else in loops unless it is a for-else:

slim rover
lunar owl
#

Hmm ic

#

So what would the solution be something like
If before for?

slim rover
#

It doesn't look like you've tested the inputs or else there would be errors coming through, all rules wouldn't pass

lunar owl
#

Not all combinations

slim rover
haughty bisonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.