I know how to write the input to a text file (Python 2.7.x).
How would you go about having pre-written text and user input on the same line?
See:
f.write("Artist: ", toart + "\n")
The error I get is:
f.write("Artist: ", toart + "\n")
TypeError: function takes exactly 1 argument (2 given)
Any help?
Change f.write("Artist: ", toart + "\n") ==> f.write("Artist: " + toart + "\n").
Try this :
f.write("Artist: " + toart + "\n")
# ^^^
This way you get only one string to be passed...
Related
The method to overwrite a line is not working anymore before, back it python 2 it was printing over a line:
For example instead of having:
1%
2%
3%
4%
The line would just update:
1% would become 2% on the same line
I have tried multiple \r methods without success: (I am running pycharm on linux)
print('\r', " we've done " + str(trying) + " try so far ")
print( " we've done " + str(trying) + " try so far ","\r")
print( " we've done " + str(trying) + " try so far ",end="\r")
I then tried the last solution with a time sleep as someone suggested it was the right one:
import time
y = 0
for i in range(100):
y += 12
print("we've done " + str(y) + " try so far", end="\r")
time.sleep(3)
But the result is that nothing is printing out. Not a single line.
This is a PyCharm specific issue. It doesn't interpret \r as a carriage return. It's raised on this issue. If you try and run it in console, you'll see that the last one
print( " we've done " + str(trying) + " try so far ",end="\r")
will work.
After several test of multiple solution i still haven't found the solution for clearing a line in Pycharm ide. Can we conclude tha we can't or is there a python solution using a librairy ?
I sometimes uses https://github.com/tqdm/tqdm which somehow is ables to overwrite lines in pycharm but i can't figure out how.
The end should be empty. default is "\n"
print(" \r we've done " + str(trying) + " try so far " , end="")
print(" \r we've done " + str(trying) + " try so far ",end="")
print(" \r we've done " + str(trying) + " try so far ",end="")
I have seen this question multiple times but none of them can apply to my code and none of it makes sense to my code. So I am sorry for asking this question.
I have written up a code where the data from previous inputs are written into a csv file. The name of the variable is called file and i have numerous of these for different inputs. But when I try to close the csv it comes up with the error:
NameError: name 'file' is not defined
Here is the code:
if classCode=="1":
file=open("class1.csv","w")
file.write("name, correct\n" + name + "," + str(correct) + "\n")
elif classCode=="2":
file=open("class2.csv","w")
file.write("name, correct\n" + name + "," + str(correct) + "\n")
elif classCode=="3":
file=open("class3.csv","w")
file.write("name, correct\n" + name + "," + str(correct) + "\n")
file.close()
I don't know why it says that 'file' isn't defined when I clearly have for all three. Am i just being stupid?
What happens if the class code is not 1,2 or 3. If all class code do the same thing you can make the filename dynamic, instead of repeating the code several times.
class_file=open("class{}.csv".format(classCode),"w")
class_file.write("name, correct\n" + name + "," + str(correct) + "\n")
class_file.close()
file variable will only declare when classCode is either "1", "2" or "3" otherwise it will not declare and error will come.
you should make sure either control goes to any of the if statement or you should declare this variable outside If statement.
I have a very simple problem that I have been unable to find the solution to, so I thought I'd try my "luck" here.
I have a string that is created using variables and static text altogether. It is as follows:
filename_gps = 'id' + str(trip_id) + '_gps_did' + did + '_start' + str(trip_start) + '_end' + str(trip_end) + '.json'
However my problem is that pylint is complaining about this string reprensentation as it is too long. And here is the problem. How would I format this string representation over multiple lines without it looking weird and still stay within the "rules" of pylint?
At one point I ended up having it looking like this, however that is incredible "ugly" to look at:
filename_gps = 'id' + str(
trip_id) + '_gps_did' + did + '_start' + str(
trip_start) + '_end' + str(
trip_end) + '.json'
I found that it would follow the "rules" of pylint if I formatted it like this:
filename_gps = 'id' + str(
trip_id) + '_gps_did' + did + '_start' + str(
trip_start) + '_end' + str(
trip_end) + '.json'
Which is much "prettier" to look at, but in case I didn't have the "str()" casts, how would I go about creating such a string?
I doubt that there is a difference between pylint for Python 2.x and 3.x, but if there is I am using Python 3.x.
Don't use so many str() calls. Use string formatting:
filename_gps = 'id{}_gps_did{}_start{}_end{}.json'.format(
trip_id, did, trip_start, trip_end)
If you do have a long expression with a lot of parts, you can create a longer logical line by using (...) parentheses:
filename_gps = (
'id' + str(trip_id) + '_gps_did' + did + '_start' +
str(trip_start) + '_end' + str(trip_end) + '.json')
This would work for breaking up a string you are using as a template in a formatting operation, too:
foo_bar = (
'This is a very long string with some {} formatting placeholders '
'that is broken across multiple logical lines. Note that there are '
'no "+" operators used, because Python auto-joins consecutive string '
'literals.'.format(spam))
I'm currently making a text game, but part of my game won't work, and I can't figure out why. This is the coding:
c1pp1 = ["Joe", "Frank", "Annie"]
while cp_ptt < 3:
s= input()
#FIX THIS!!
c1pp = input(n/ + ": Who should I talk to first?\nYour options are:\n" +\
c1pp1[0] + ", an ex-policeman\n" + c1pp1[1] + ", a carpenter\n" +\
c1pp1[2] + ", an architect\n")
The outcome I get is:
Traceback (most recent call last):
File "N:\MyWork\Year 9\Other\Random\Making a text game out of song lyrics\No.1;
Phantom Thief F's Scenario ~Mystery of the Missing Diamond~.py", line 103,
in <module>
c1pp = input(n/ + ": Who should I talk to first?\nYour options are:\n" + c1pp1[0] + ", an ex-policeman\n" + c1pp1[1] + ", a carpenter\n" + c1pp1[2] + ", an architect\n")
TypeError: bad operand type for unary +: 'str'
Can someone please tell me what is wrong with my code?
Some parentheses to highlight the expression will help.
Your code is trying to parse n / (+ ": Who should I talk to first?\nYour options are:\n") but + "any string" is not a valid use of the unary + operator.
Like others have pointed out in the comments, you've probably made a typo. Without the rest of your code, I can't tell you what you need to do to get your desired output, but you can probably start by removing that /.
I am making an average calculator in python, i have to save the averages in a text file and have done it like this:
Mean = statistics.mean(aver)
Mode = statistics.mode(aver)
Medi = statistics.median(aver)
file = open("Averages.txt", "a")
file.write("\n\nYour numbers are" + aver +
"\nMean : " + Mean +
"\nMode : " + Mode +
"\nMedian : " + Medi)
(aver is a list of numbers i am finding the average of)
when i try to run this part of the code, i recieve the error message:
TypeError: Can't convert 'list' object to str implicitly
I tried basic stuff like adding 'str' on but it doesnt help.
file.write("\n\nYour numbers are" + **aver** +
this would be better as something like this:
aver = " " + ", ".join(aver) + " "
which converts your list to a comma separated string.