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.
#1
Hi all!
I just started learning python3.
I usually don't come to forums for help cause I'm a "do it your self" kinda a guy :D
This one has been breaking my balls over 4h now :/

"Create a program which reads all of the lines from the file and tests the lines. If the line has only letters and/or numbers, the program prints "[line] was ok.". If the line has special characters, the program should print "[line] was invalid.". When the program works, it prints out something like this:"

>>> 
5345m345ö34l was ok.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
bbrbwb55be3"?"#? was invalid.
"?"#%#"!%#"&"?%%"?#?#"?" was invalid.
retrte#%#?% was invalid.
abcdefghijklmnopqrstuvxy was ok.
>>> 
this is what i got right now.


f = open("strings.txt")
line = f.readline()
for line in f:
    if "@"or "#" or "$" or "%" in line[0]:
        print(line," was invalid.")
    else:
        print(line," was ok.")
   


strings.txt contains the following:


Quote:5345m345ö34l .
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
bbrbwb55be3"?"#?
"?"#%#"!%#"&"?%%"?#?#"?"
retrte#%#?%
abcdefghijklmnopqrstuvxy

Brb in 30min, need to go inhale some fresh air Wall
ah forgot to post this, @ top


Quote:"It is advisable to read the lines one at a time, test them with the isalmun() string test and go on from there. Also remember that the strings may also end in a line break (\n), which is allowed, but fails the .isalnum() test if not sliced away."

@wavic
In my lessons i havent gotten far enough to use "import"
iv gone through:   if,else,break,skip,while,for,skip,write,read
Reply
#2
This is a problem a lot of new users run into.
Check out Multiple expressions with "or" keyword for details on what's causing it and how to fix it.
Reply
#3
f = open("strings.txt")
line = f.readline()
invalid = ("!","@","#","$","%","^","&","*","(",")","_","+","=","-")

for line in f:
    if line in invalid:
        print(line," was invalid.")
    else:
        print(line," was ok.")
    
still doesn't work
Reply
#4
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.

A couple of ways you could do this are:
  1. set intersection (simplest in terms of code needed, but you need to be familiar with python's sets)
  2. for loop (for each character in line, check if it's in your list).
If you decide to use the loop approach, I'd recommend wrapping the checking logic inside a function.
Reply
#5
In [1]: import string

In [2]: string.punctuation
Out[2]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

In [3]: 
You can check for each of these in for loop
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
still breaking my ballsLOL  
Reply
#7
(Nov-27-2016, 04:59 PM)Jei Wrote: still breaking my ballsLOL

That's not really enough information to help you further.
Show your latest code, and let us know the details of what's wrong.
Reply
#8
(Nov-27-2016, 06:04 PM)stranac Wrote:
(Nov-27-2016, 04:59 PM)Jei Wrote: still breaking my ballsLOL

That's not really enough information to help you further.
Show your latest code, and let us know the details of what's wrong.

only have this atm :
f = open("strings.txt")
l = f.readline()
w = ("!","@","#","$","%","^","&","*","(",")","_","+","=","-",".",",")
now i only need to start reading lines one by one and check if its valid or not Wall
any tips? Pray
Reply
#9
Stranac gave you the tips you need. One way to do this is with a set intersection. If you are not familiar with sets, it will probably be easier to do a loop. Either loop through the characters in w, seeing if they are in l; or loop through the characters in l, seeing if they are in w. The former would probably be more efficient.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
f = open("strings.txt","r")
for line in f.read().split("\n"):
    if line in ("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "=", "-", ".", ","):
        print(line, "was invalid")
    else:
        print(line, "was ok")
f.close()
why is it only using "else:?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Some exercises I need help with. Beginner_coder123 2 2,817 Dec-02-2018, 06:21 PM
Last Post: ichabod801
  Mission Impossible : New to Python and 3 days to do theses exercises Kangaaxx 6 3,736 Aug-16-2018, 05:58 PM
Last Post: Kangaaxx
  Python, breaking words Folija 4 3,638 Nov-18-2017, 10:04 PM
Last Post: Folija
  3 Finance Python Exercises mmkthen 2 5,403 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