Selective printout from file [duplicate] - python

This question already has answers here:
Printing a function in Python
(5 answers)
Closed 3 years ago.
I'm trying to write program, who's printout specific words (without "e"). But i have a problems.
that's my code:
def has_no_e(fin,word):
fin = open('words.txt')
for line in fin:
word = line.strip()
if 'e' not in word:
print(word)
else:
continue
print(has_no_e)
Pycharm after run it printout that:
function has_no_e at 0x00E078A0
I don't know what's wrong. Thanks everybody for any help.

Try has_no_e(<fin>, <word>), without the print. What you're doing there is printing the function address itself, if you try doing has_no_e(<fin>, <word>) the function contents will be executed instead, which is what you want.
P.S. Replace fin and word with the actual parameters you want to pass.

Related

Outputting argument but no new line feature in Python [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
Is there a way how to remove the property of the print() function in Python that makes end of line after outputting its argument? Or is there a different function to do the output of argument but no the end line?
To illustrate this, lets say I want to output the English alphabet on one line. The following code prints every letter on a new line:
for x in range(26):
print(chr(x+97))
While the second code prints it the way requested, but by concatenation of strings instead:
s=''
for x in range(26):
s+=chr(x+97)
print(s)
print(“Hello”, end=“”)
print(1)
Output: Hello1
Hope this helps :) the default value of end is “\n” and you can change it to anything you want.

Trouble using Equality operator to check if a user input is the same as a line from a text file in Python [duplicate]

This question already has an answer here:
compare lines in a file with a string
(1 answer)
Closed 5 years ago.
Just started to learn Python again since finishing my GCSEs two years ago. I've got a bit of background in C# but am having difficulties making a simple program work.
The program is a quiz. It takes input from a text file and stores each line into a variable. Then the program goes on to take input in the form of an answer however when I enter the correct answer to the question,the equality operator does not recognise the user input and line from the text file as the same.
The textfile is formatted through Question on first line, answer on second and so on.
Heres what I have so far:
quiz = open("cseasy.txt","r")
wholequiz = quiz.readlines()
q1 = wholequiz[0]
a1 = wholequiz[1]
mark = 0
ua1 = input(q1)
print (ua1)
print(a1)
if a1 == ua1:
print ("Correct!")
mark = mark + 1
else:
print ("Incorrect!")
I'd be grateful for any solutions and why the current program is not producing the result I want.
The above answer to q1 is A, yet when I enter "A" the output of the program is "Incorrect!".
with
wholequiz = quiz.readlines()
you get a list of lines with newline (\n) at the end. The comparison cannot succeed unless you do:
a1 = wholequiz[1].rstrip()
That method is useful when you're reading line by line, but since you're reading the whole file at once, you could also do:
wholequiz = quiz.read().splitlines()
then no need to strip for linefeed. Of course, if there's a trailing space in the file that won't remove it (the rstrip method does)

Removing printed lines in Python 3 [duplicate]

This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().

Checking if text file is empty Python [duplicate]

This question already has answers here:
How to check whether a file is empty or not
(11 answers)
Closed 6 years ago.
Is there a way to check if a text file is empty in Python WITHOUT using os ?
What I have tried so far
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
But I don't think it's the correct way.
Not sure why you wouldn't use os, but I suppose if you really wanted you could open the file and get the first character.
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something

How to print the codes in python? [duplicate]

This question already has an answer here:
Print out all code in a Python script
(1 answer)
Closed 8 years ago.
How does one print the lines of code?
Suppose we have here two variables.
var1 = 2*8
msg = "Answer is: "
What statement should i add here so that this program will print the source code?
The easiest way to print the lines of codes is through the use of the built-in functions.
print open(__file__).read()
Or you could just write the code in string mode and simply print them. However that obviously won't be executable codes anymore once written in quotation marks.

Categories

Resources