扫描字符串文字时的Python EOL
我试图创建一个用于类项目的输出文件,但在扫描这一行的字符串文字时,它始终给出一个下线:扫描字符串文字时的Python EOL,python,Python,我试图创建一个用于类项目的输出文件,但在扫描这一行的字符串文字时,它始终给出一个下线:outfile=open(r'C:\Users\kay\Documents\CCA Classes\CIS 119\numbers.txt',w')。我知道“\”是一个特殊字符,但我的书中说r前缀使其成为一个原始字符串,这应该使其成为文件位置。我做错了什么 #This program creates the number file import os def main(): #Get how man
outfile=open(r'C:\Users\kay\Documents\CCA Classes\CIS 119\numbers.txt',w')
。我知道“\”是一个特殊字符,但我的书中说r前缀使其成为一个原始字符串,这应该使其成为文件位置。我做错了什么
#This program creates the number file
import os
def main():
#Get how many numbers from the user
many_num = int(input("Enter how many numbers ' + \
you will be adding: '))
#Open a file for writing
outfile = open(r'C:\Users\kay\Documents\CCA Classes\CIS 119\numbers.txt', 'w'")
#Get the numbers and write to the file
for count in range(1,many-num +1):
numbers = int(input('Enter a number: ' + \
str(count) + ': '))
#write number to file
outfile.write(str(numbers) + '\n')
#Close the file
outfile.close()
print('Data written to numbers.txt.')
#Call main function
main()
比较这两行,第二行是正确的(您可以使用双引号或单引号,这无关紧要)。通过使用双引号,您将两个参数作为一个字符串传递,其中包含单引号和逗号:
#outfile = open(r"C:\Python33\numbers.txt', 'w'")
outfile = open(r'C:\Python33\numbers.txt', 'w')
您将添加:'
-->您将添加:'
感谢您的关注!我把它放进去,它仍然会给我下线错误,并突出显示(就在open@Maggi3339336选中引号,这行字符串以双引号开始,以单引号结束:`many_num=int(输入(“输入多少个数字”+`