New line (/n) not working in Python 2 [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I tried to make a power calculator program in Python 2.7. It worked but I tried to write the values into a file and the /n didn't work. Here is the program:
import math
file = open("numbers.txt" , 'w')
c = 0
a = int(raw_input("A number: "))
b = int(raw_input("To the power "))
h = range(b)
h.append(b)
print 1
file.write('1')
for c in range(b):
print int((math.pow(a, h[c+1])))
k = (int((math.pow(a, h[c+1]))))
file.write((str(k)+"/n") `

You're using a normal slash (/). But you need to use a Backslash (\).
So, \n will add a new line.
\ is used for escape sequence.

Related

Python question: My function isn't running, using the input values I have set. Python 3.9 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am trying to create a function to convert DMS to DD. My function isn't running utilizing the import answer I provide. I an new to python, any assistance would be great.
import math
def d_m_s(d,m,s):
d = int(d)
m = int(m)
s = int(s)
dd = d + m/60 + s/3600
print(dd)
d,m,s = input("Enter degrees, minutes, and seconds:").split(',')
print(type(d))
You have called the type() function in your print statement. This will just tell you if d is a string, int, float, etc. In order to print "d", just use print(d).
However, in order to print the DD conversion, instead of ending with a print statement, you can replace the last line with d_m_s(d,m,s)
Hope this helps!

"starts with" does not work (python regex) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have this string:
m = 'Film/6702-BRINGING-UP-BABY" da'
and the following regax, that tries to find words that start with 'd':
movie = re.findall(r'^d.*', m)
print(movie)
the result is:
[]
and I can not understand why - there is a match in the string('da'), so why wont it find it?
I an working with pycharm, python 3.6
Using \b as boundary character we can find the words starting with specific character
import re
m = 'Film/6702-BRINGING-UP-BABY" da'
movie = re.findall(r'\bd.*', m)
print(movie)
output
['da']

Python If(and) statement syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to make a simple console game in Python 2.7, where the user gets a dilemma and makes a decision by typing the number correlating to the option he wants to choose. This question is the main menu.
s = raw_input("Enter a number in the range 1 to %s\n> " % v
if (is_number(s) and s in q):
return s
I'm getting a "SyntaxError: Invalid syntax" from the if (is_number(s) and s in q) statemet when trying to run the program. It was working fine before i added the question.
This is my first real program.
You're missing a parentheses on the previous line:
s = raw_input("Enter a number in the range 1 to %s\n> " % v
# here--^

Python - Syntax error with nested iterations? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SO I have this code:
Chars = maketrans(" ABCDEFGHIJKLMNOPQRSTUVWXYZ-.,"," ABCDEFGHIJKLMNOPQRSTUVWXYZ-.,");
input = input.split(" ");
length = len(input);
charLength = len(Chars);
for x in range(1,length):
for y in range(1,charLength):
for z in range(MinInt,MaxInt):
if Transform(z + x.translate(Chars) + Key)[:5] == input[x]
print x.translate(Chars)
The function receives blocks of 5 characters separated by spaces. When attempting to run it, I get the following error:
File "SH25.py", line 21
if Transform(z + x.translate(Chars) + Key) == input[x]
^
SyntaxError: invalid syntax
I'm admittedly a newbie at Python, but could anyone help? Thanks.
The error message is pretty precise: you need a : after if
if Transform(z + x.translate(Chars) + Key)[:5] == input[x]:

Python in proccess of geting to pig game [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.

Categories

Resources