#๐Ÿ”’ how do i convert this code so it stops when a single "code non valide" happens using elif

56 messages ยท Page 1 of 1 (latest)

merry waspBOT
#

Hey @alpine pier!

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!')```
#

@alpine pier

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.

alpine pier
#
ch=str(input("code du produit?"))
a=ch.find("#")
if a==-1:
 print("code non valide")
ch1=ch[:a]
ch2=ch1[:1]
if ch1<"A" or ch1> "Z":
    print("code non valide")
ch3=ch1[1:a]
if ch3.isdecimal() != True:
    print("code non valide")
ch4=ch[a+1:]
b=ch4.find("#")
if b==-1:
    print("code non valide")
ch5=ch4[0:b]
if ch5.isdecimal() != True and len(ch5)>10:
    print("code non valide")
c=ch4.find("#")
ch6=ch4[c+1:]
if not int(ch6)>=1:
    print("code non valide")```
#

@tribal badge sorry for the pin

#

heres the finished code

#

now i js need to use elif

#

idk how tho

tribal badge
#

it would look like this

alpine pier
#

wait lets say i do the the non if statements first

#

then i check the conditions

tribal badge
#
if condition:
    something
elif condition:
    something
alpine pier
#

then i use if and elif to check the conditions

tribal badge
#

you can't do

if condition:
    something
another something
elif condition:
    something
alpine pier
#

and end off with else

#

for code valid

#

yea i think that can work

#

lemme do it

tribal badge
alpine pier
tribal badge
#

or you can indent them

alpine pier
#

lemme js copy them here so i dont have to type them again

tribal badge
#

in theory its better to get them as you need them. For example if you find ch1 and then the first condition makes the code invalid, then there's no point finding ch2 ch3 etc. Its a waste of processing time

#

in this case it doesn't matter because it takes less than a second but inefficiencies stack up

alpine pier
#

if a==-1:
print("code non valide")

if ch1<"A" or ch1> "Z":
print("code non valide")

if ch3.isdecimal() != True:
print("code non valide")

if b==-1:
print("code non valide")

if ch5.isdecimal() != True and len(ch5)>10:
print("code non valide")

if not int(ch6)>=1:
print("code non valide")

merry waspBOT
#

Hey @alpine pier!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
alpine pier
#

idk how to do it as u said in this message

#

its good enough for now

#

isnt that a motto for yall

#

๐Ÿ˜ญ

tribal badge
alpine pier
tribal badge
#

okay ping me if you're stuck

alpine pier
#

@tribal badge it works

#

yippee

#
ch=str(input("code du produit?"))
a=ch.find("#")
ch1=ch[:a]
ch2=ch1[:1]
ch3=ch1[1:a]
ch4=ch[a+1:]
b=ch4.find("#")
ch5=ch4[0:b]
c=ch4.find("#")
ch6=ch4[c+1:]
if a==-1:
 print("code non valide")
elif ch1<"A" or ch1> "Z":
    print("code non valide")
elif ch3.isdecimal() != True:
    print("code non valide")
elif  b==-1:
    print("code non valide")
elif ch5.isdecimal() != True and len(ch5)>10:
    print("code non valide")
elif not int(ch6)>=1:
    print("code non valide")
else:
    print ("code valide")```
#

why was i stuck on this for 2 hours

tribal badge
#

you can also just do something like

#

actually nevermind

alpine pier
#

we still havent learned that in class

#

and the teacher will mark it wrongg

tribal badge
#

yeah it doesn't matter i was going to say something else

#

your spacing is a little bit funny but it should be fine

#

like extra whitespace or not enough spaces in some parts

#

but its fine

#

thats just style

alpine pier
#

i do that to make it less cluttered for my eyes

#

@tribal badge anyways uh

#

thanks for the help

#

ts took me way too long

#

๐Ÿ˜ญ

#

!close

merry waspBOT
#
Python help channel closed with !close

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.