我正在尝试编制一个在python中销售剧院门票的模拟。该计划将在填写成人,儿童和老年人数量后计算总收入,但检查收入和座位百分比总是显示为0。 我有与percentage_sold和sum_income有关的问题。 任何人都可以帮助我吗?
## -*- coding: iso-8859-15 -*-
import os
import sys
from tkinter import *
from tkinter import ttk
class Theater:
def __init__(self, name, seats, price_adult, price_child, price_senior):
self.name = name
self.seats = seats
self.price_adult = price_adult
self.price_child = price_child
self.price_senior = price_senior
self.sold_adult = IntVar(root)
self.sold_child = IntVar(root)
self.sold_senior = IntVar(root)
self.percentage_sold = IntVar(root)
self.sum_income = IntVar(root)
def __str__(self):
return self.name
root = Tk()
root.title("Teater ekonomi")
def create_list(FILENAME):
f = open(FILENAME, "r")
theater_list = []
parts = []
for line in f:
parts = line.strip().split('/')
theater_list.append(Theater(parts[0], int(parts[1]),
int(parts[2]), int(parts[3]), int(parts[4])))
f.close()
return theater_list
def calculate_profits(theater_list):
for thing in theater_list:
thing.percentage_sold = int(((thing.sold_adult
+ thing.sold_child + thing.sold_senior) / (float)thing.seats) * 100)
thing.sum_income = ((thing.sold_adult * thing.price_adult)
+ (thing.sold_child * thing.price_child) + (thing.sold_senior * thing.price_senior))
FILENAME = "teatrar.txt"
error_message = ""
if os.path.isfile(FILENAME):
try:
theater_list = create_list(FILENAME)
except ValueError:
error_message = "Filen 'teatrar.txt' är korrupt.\nProgrammet kan inte fortsätta."
else:
error_message = "Filen 'teatrar.txt' existerar inte eller är inte i programmappen.\nProgrammet kan inte fortsätta."
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
if error_message != "":
ttk.Label(mainframe, text=error_message, justify="center").grid(column=1, row=1, sticky=(W, E))
else:
theater_names_col = []
theater_ticket_one = []
theater_ticket_two = []
theater_ticket_three = []
theater_perc_row = []
theater_profits_row = []
ttk.Label(mainframe, text="Teatrar").grid(column=1, row=1, sticky=(W))
ttk.Label(mainframe, text="Vuxenbiljetter").grid(column=2, row=1, sticky=(W))
ttk.Label(mainframe, text="Barnbiljetter").grid(column=4, row=1, sticky=(W))
ttk.Label(mainframe, text="Pensionärbiljetter").grid(column=6, row=1, sticky=(W))
ttk.Label(mainframe, text="Procent fullsatt").grid(column=8, row=1, sticky=(W))
ttk.Label(mainframe, text="Inkomst för kvällen").grid(column=10, row=1, sticky=(W))
for x, y in enumerate(theater_list):
ttk.Label(mainframe, text="st").grid(column=3, row=x+2, sticky=(W))
ttk.Label(mainframe, text="st").grid(column=5, row=x+2, sticky=(W))
ttk.Label(mainframe, text="st").grid(column=7, row=x+2, sticky=(W))
ttk.Label(mainframe, text="%").grid(column=9, row=x+2, sticky=(W))
ttk.Label(mainframe, text="kr").grid(column=11, row=x+2, sticky=(W))
theater_names_col.append(ttk.Label(mainframe, text=y.name).grid(column=1, row=x+2, sticky=(W,E)))
theater_ticket_one.append(ttk.Entry(mainframe, textvariable=y.sold_adult, justify="right").grid(column=2, row=x+2, sticky=(W,E)))
theater_ticket_two.append(ttk.Entry(mainframe, textvariable=y.sold_child, justify="right").grid(column=4, row=x+2, sticky=(W,E)))
theater_ticket_two.append(ttk.Entry(mainframe, textvariable=y.sold_senior, justify="right").grid(column=6, row=x+2, sticky=(W,E)))
theater_perc_row.append(ttk.Label(mainframe, textvariable=y.percentage_sold, justify="right").grid(column=8, row=x+2, sticky=(W,E)))
theater_profits_row.append(ttk.Label(mainframe, textvariable=y.sum_income, justify="right").grid(column=10, row=x+2, sticky=(W,E)))
root.mainloop()
答案 0 :(得分:1)
听起来你需要确保你做浮动除法而不是整数除法。最简单的方法是将其中一个操作数转换为float:
# instead of
seats_filled = seats_sold / total_seats
# have
seats_filled = seats_sold / float(total_seats)
除了float
的问题,使用IntVar
,StringVar
,BooleanVar
和DoubleVar
时,您必须使用.set(xxx)
进行更改值.get()
以检索当前值。