#๐Ÿ”’ Else statement that keeps on printing!

45 messages ยท Page 1 of 1 (latest)

cyan onyx
#

Currently on week 1 of CS50P. I have an else statement that keeps on printing even though the scenario is true, and wondering what I could implement to avoid this problem. I know that I didn't add a proper conditional to it, it has repetitive code, such as if and endswitch as well.

extensions_files = input("File name: ")

extensions_files = extensions_files.replace(" ", "")

if extensions_files.endswith(".jpg"):
print("image/jpg")

if extensions_files.endswith(".jpeg"):
print("image/jpeg")

if extensions_files.endswith(".pdf") or ("PDF"):
print("application/pdf")

if extensions_files.endswith(".png"):
print("image/png")

if extensions_files.endswith(".gif"):
print("image/gif")

if extensions_files.endswith(".txt"):
print("text/plain")

elif extensions_files.endswith(".zip"):
print("application/zip")

else:
print("application/octet-stream")

royal flareBOT
#

@cyan onyx

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.

raven narwhal
royal flareBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
cyan onyx
#

Oh, I completely forgot about that! Thank you, I also never knew about the reverse word. Thanks.

#

Looks better? if extensions_files.endswith(".png") or extensions_files.endswith("PDF"):
print("image/png")

vast acorn
#

pdf isn't image

#

str.endswith actually allows you to provide multiple options

#
if extensions_files.endswith(('.pdf', '.PDF'))
#

you could also do:

if extensions_files.lower().endswith('.pdf')
clear dawn
#

Main problem with your code is actually that those ifs are all separate.
Until last if-elif-else - those three are grouped. But only those
So the else will always print unless that if or that elif is True. Any other if? This group doesn't care about them

#

!e

a = 1
if a==1:
    print("one")
if a==2:
    print("two")
else: # <-this is connected only to the previous if
    print("many")
royal flareBOT
cyan onyx
#

Shouldn't it be: if extensions_files.lower().endswith('.PDF')?

clear dawn
vast acorn
cyan onyx
#

How about checking if it is capitalized?

vast acorn
#

.upper()

#

if you want to check if something is capitalized, it's .isupper()

cyan onyx
#

Thanks!

#

I'll make these changes, thank you guys! Is there anything I could do to reduce this code?

vast acorn
#

Yes, but unless you've learned about dictionaries already, I wouldn't worry about it just yet

cyan onyx
#

No, I haven't. Thank you once again.

cyan onyx
cyan onyx
vast acorn
vast acorn
cyan onyx
vast acorn
#

if you do .upper(), then check for "PDF". If you do .lower(), then check for "pdf"

#

it doesn't really matter which one you pick, just make sure you're consistent

cyan onyx
#

Okay

cyan onyx
# clear dawn Main problem with your code is actually that those ifs are all separate. Until l...

extensions_files = input("File name: ")

extensions_files = extensions_files.replace(" ", "")

if extensions_files.endswith(".jpg") or extensions_files.endswith(".jpeg"):
print("image/jpeg", end="")

elif extensions_files.lower().endswith('.pdf'):
print("application/pdf")

elif extensions_files.endswith(".png"):
print("image/png")

elif extensions_files.endswith(".gif"):
print("image/gif")

elif extensions_files.endswith(".txt"):
print("text/plain")

elif extensions_files.endswith(".zip"):
print("application/zip")

else:
print("application/octet-stream")

royal flareBOT
#

Hey @cyan onyx!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
vast acorn
#
if extensions_files.endswith(('.jpg', '.jpeg'))
cyan onyx
#

Oh, great!. I will add that since it is shorter. Thank you guys for your help. This might be off topic, but I'm wondering how you understand Python so well?

vast acorn
#

time, practice, experience

#

you're 1 week in

#

I'm 7 years in

cyan onyx
#

That's impressive. Thanks once again.

vast acorn
royal flareBOT
#
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.