Python Forum
One of my exercises is breaking my balls.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
One of my exercises is breaking my balls.
#11
If line == '@1' it's not in your list
if line == '$%' it's not in your list
if line == 'This is my %^$ line' it's not in your list
if line == '@' it is in your list
Reply
#12
(Nov-28-2016, 03:19 PM)Jei Wrote: why is it only using "else:?

(Nov-27-2016, 04:03 PM)stranac Wrote: Right now you're checking if a longish string, such as "no2no123non4" is in your list of single character strings.
This will never be the case.
(Nov-28-2016, 02:33 PM)ichabod801 Wrote: Stranac gave you the tips you need.
Reply
#13
strings = ("!","@","#","$","%","^","&","*","(",")","_","+","=","-",".",",",")

with open("strings.txt") as f:
    for line in f:
        if any(i in line for i in strings):
            print(line.strip('\n') + " was invalid.")
        else:
            print(line.strip('\n') + " was ok.")
f.close()
Angel
Reply
#14
If you're using a with block like that, you don't need to close the file manually.  any() is kind of advanced for where it seems you are, you could also iterate over the contents of the line, like so (normally I don't post answers to questions, but since you already have a working solution, I'll offer some tips):
# ...open file, define invalid chars, etc
for line in input:
    valid = True
    for char in line:
        if char in invalid_characters:
            valid = False
        # or...
        valid = valid and char not in invalid_characters

    if valid:
        print("{0} was ok".format(line))
    else:
        print("{0} was invalid".format(line))
    # or...
    print("{0} was {1}".format(line, "ok" if valid else "invalid"))
Reply
#15
(Nov-30-2016, 05:46 PM)nilamo Wrote: If you're using a with block like that, you don't need to close the file manually.  any() is kind of advanced for where it seems you are, you could also iterate over the contents of the line, like so (normally I don't post answers to questions, but since you already have a working solution, I'll offer some tips):
# ...open file, define invalid chars, etc
for line in input:
    valid = True
    for char in line:
        if char in invalid_characters:
            valid = False
        # or...
        valid = valid and char not in invalid_characters

    if valid:
        print("{0} was ok".format(line))
    else:
        print("{0} was invalid".format(line))
    # or...
    print("{0} was {1}".format(line, "ok" if valid else "invalid"))

Thanks for the tips Rolleyes
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Some exercises I need help with. Beginner_coder123 2 2,805 Dec-02-2018, 06:21 PM
Last Post: ichabod801
  Mission Impossible : New to Python and 3 days to do theses exercises Kangaaxx 6 3,718 Aug-16-2018, 05:58 PM
Last Post: Kangaaxx
  Python, breaking words Folija 4 3,626 Nov-18-2017, 10:04 PM
Last Post: Folija
  3 Finance Python Exercises mmkthen 2 5,395 Oct-29-2017, 02:55 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020