我正在执行以下代码:
def filewrite():
filew = open('company.dat', 'a')
custid = str(input('Enter Customer ID:'))
Widg = abs(float(input('Quantity of Widgets Ordered:')))
Gidg = abs(float(input('Quantity of Gidgets Ordered:')))
Dood = abs(float(input('Quantity of Doodads Ordered:')))
widt = Widg * 10.55
gidt = Gidg * 7.30
doodt = Dood * 5.25
total = widt + gidt + doodt
wida = (Widg, 'Widgets Ordered @ $10.55', format(widt,',.2f'))
gida = (Gidg, 'Gidgets Ordered @ $7.30', format(gidt,',.2f'))
dooda = (Dood, 'Doodads Ordered @ $5.25', format(doodt,',.2f'))
filew.write('\n')
filew.write(custid + '\n')
filew.write(str(wida))
filew.write(str(gida))
filew.write(str(dooda))
filew.close()
def fileread():
filer = open('company.dat', 'r')
read = filer.read()
filer.close()
print(read, end='')
again = True
while again == True:
print('1 - Add a new Invoice Order')
print('2 - Display all Invoice Orders')
print('3 - Quit')
choice = abs(int(input('Enter 1, 2, or 3:')))
if choice > 3:
print('Error, no choice of such available.')
choice = abs(int(input('Enter 1, 2, or 3:')))
if choice == 1:
filewrite()
print('\n')
if choice == 2:
fileread()
print('\n')
if choice == 3:
exit()
当我输入所有内容并读取文件时,它显示为:
As234
(12.0, 'Widgets Ordered @ $10.55', '126.60')(1.0, 'Gidgets Ordered @ $7.30',
'7.30')(3.0, 'Doodads Ordered @ $5.25', '15.75')
如何摆脱显示的()和''?
我是编码和入门python类的新手,这是我的任务之一,我对此很执着。
答案 0 :(得分:4)
您的问题是如何创建变量。 在这里,您将一些变量创建为整数:
Widg = abs(float(input('Quantity of Widgets Ordered:')))
Gidg = abs(float(input('Quantity of Gidgets Ordered:')))
Dood = abs(float(input('Quantity of Doodads Ordered:')))
但是在这里,您使用每个变量列出一个列表:
wida = (Widg, 'Widgets Ordered @ $10.55', format(widt,',.2f'))
gida = (Gidg, 'Gidgets Ordered @ $7.30', format(gidt,',.2f'))
dooda = (Dood, 'Doodads Ordered @ $5.25', format(doodt,',.2f'))
然后,当您将其转换为字符串str(wida)
时,您将得到确切的打印内容,因为这就是为转换提供的内容。
您应检查要创建的变量类型,然后执行以下操作之一:
如果您对第二个选项有任何疑问,请评论或阅读documentation。
答案 1 :(得分:0)
就像@ usr2564301所说的那样,实现目标的最简单方法是首先不要使用它们。
如果您必须(并且我不建议这样做),则可以替换Python字符串中的字符。必须注意,st是不可变的,因此必须创建一个新的字符串。
x = "(12.0, 'Widgets Ordered @ $10.55', '126.60')(1.0, 'Gidgets Ordered @ $7.30', '7.30')(3.0, 'Doodads Ordered @ $5.25', '15.75')"
a = x.replace("(", "")
b = a.replace(")", "")
c = b.replace("'", "")
print(c)
返回
12.0, Widgets Ordered @ $10.55, 126.601.0, Gidgets Ordered @ $7.30, 7.303.0, Doodads Ordered @ $5.25, 15.75