多个输入并将结果发送到文本

时间:2017-10-26 12:43:06

标签: python input io

请不要只回答。我是新人,这是我的学习方式。如果你可以花些额外的时间来解释我为什么或如何欣赏它。

我需要获得多个输入,我希望用户输入:ID,金额,小费金额。金额和小费的多个实例。然后移植到文本文件 与此格式类似:日期,员工ID,金额,金额总额,小费金额,小费总额。

## Program gets the date, Employee ID, and amounts.  
## It then places them into a text file which can be ported to excel.

## Get Date


## Get Employee I.D.
empnum = raw_input('Please enter Employee Number\n')

## Gets the amounts.
num1 = raw_input('Please enter Ticket amount.\n')
num2 = raw_input('Please enter tip amount.\n')

## Sum of the amounts.
sum = float(num1) + float(num2)

## Information to be ported to text file.
## Date, Employee ID, amount, Total sum of amount, tip amount, total sum of tip
print'Employee ' + empnum + ' total is' 
print(sum)

1 个答案:

答案 0 :(得分:0)

您必须阅读所需数据类型的输入。

empnum = raw_input('Please enter Employee Number\n')
num1 = float(raw_input('Please enter Ticket amount.\n'))
num2 = float(raw_input('Please enter tip amount.\n'))

现在您需要做的是在写入模式下使用open函数打开文件处理程序。

fle =open("C:\Python27\projects\infile.txt",'w')

进行计算

sum = num1 + num2

而不是打印,您需要使用write函数

将其写入文件
fle.write('Employee ' + empnum + ' total is ' + str(sum))

然后关闭文件

fle.close()