我正在尝试创建一个可用于输入数据和保留记录但无法弄清楚如何减去两个变量的程序,我是编码的新手,谢谢您的帮助:
ans = str
gram = int
bprice = int
sprice = int
totalg = int
balance = int
totalc = int
timesb = int
timess = int
profit = balance - totalc
while ans != "buy" or "sell" or "enter" or "check":
print ("Would you like to Buy, Sell, Enter Variables or Check Records ?")
ans = input()
if ans == "buy" :
print ("How many grams?")
gram = input()
print ("How much did you pay for it?")
bprice = input()
totalg = int(totalg) + int(gram)
balance = int(balance) - int(bprice)
totalc = int(totalc) + int(bprice)
timesb = timesb + 1
if ans == "sell" :
print ("How many grams?")
gram = input()
print ("How much did you sell it for?")
sprice = input()
totalg = int(totalg) - int(gram)
balance = int(balance) + int(sprice)
timess = timess + 1
if ans == "enter" :
print ("What is the starting balance?")
balance = input()
if ans == "check" :
print ("Balance = £",balance)
print ("Total cost = £",totalc)
print ("Stock left = ",totalg,"g")
print ("Profit = £",profit)
print ("Times bought = ",timesb)
print ("Times sold = ",timess)
答案 0 :(得分:0)
您必须初始化变量,否则会出错。请注意您的while条件,您应该放置“ and”而不是or。这是购买条件的工作代码:
__eq__