我尝试了一段代码并且它运行良好,但问题是它有一个我想要的行中的file.write值。但它不起作用。 代码是:
start = input('Enter your starting point:')
stop = input('enter your destination:')
count = input('Ticket count:')
p = 10 * int(count) #a + b
q = 20 * int(count)#a + c
r = 30 * int(count)#a + d
s = 10 * int(count)#b + c
t = 20 * int(count)#b + d
u = 10 * int(count)#c + d
if start == 'a' and stop == 'b':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(p))
file.close()
print ('Your cost is usd ',p)
elif start == 'a' and stop == 'c':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(q))
file.close()
print ('Your cost is usd ',q )
elif start == 'a' and stop == 'd':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(r))
file.close()
print ('Your cost is usd ',r )
elif start == 'b' and stop == 'c':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(s))
file.close()
print ('Your cost is usd ',s )
elif start == 'b' and stop == 'd':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(t))
file.close()
print ('Your cost is usd ',t )
elif start == 'c' and stop == 'd':
file = open(r"C:\Users\the\Desktop\py\2.txt " , 'w+')
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(u))
file.close()
print ('Your cost is usd ',u )
else:
print('you entered wrong values')
请给我一个解决方案或修复代码。有一点要提到我在python中的新手,这是我尝试的第一个程序。希望能解决这个问题。
答案 0 :(得分:1)
我希望这段代码以单独的行写入,你必须在每行的末尾添加一个“\ n”:
file.write('Location: '+ start +' > '+ stop +\
'Ticket count: '+ count +\
'Total fare: ' + str(r)+"\n")
对于Windows,您可能需要“\ r \ n”