Python脚本在IDLE Shell和Bash中以.py而不是.exe的形式运行

时间:2019-06-04 03:23:22

标签: python string dictionary executable

我正在尝试在字典中查找值,检查它们是否满足条件(超过数字限制),然后从字典中删除键:值对。这在IDLE Shell和Bash中工作正常,但是一旦我隐蔽了文件并尝试以可执行文件身份运行,就会卡住并抛出错误代码,指出字符串无法转换为浮点型。程序启动,但在其中一本词典上调用“限制器”时挂断。

我尝试了pyinstaller并将.py更改为.command(mac)(chmod + x file.command,尝试使用int()。

#brings str1 and str2 into single dictionary
def dictionizer(str1, str2):
  str1 = str1.strip().split('\n')
  str2 = str2.split('\n')
  str2 = filter(None, str2)
  d = dict(zip(str1, str2))
  return d

##brings dicitionaries together
def merge(d1, d2):
  merged = d1.copy()
  merged.update(d2)
  return merged

#synthesizes list of strings into dictionaries
d_burgers = dictionizer(burgers, burgers_kcal)
d_breakfast = dictionizer(breakfast, breakfast_kcal)
d_chicken = dictionizer(chicken, chicken_kcal)
d_salad = dictionizer(salad, salad_kcal)
d_fries = dictionizer(fries, fries_kcal)

#creates finds all values above/below limit, removes qualifying       key:value pairs
def limiter(d):
  for k,v in list(d.items()):
    if float(v) >= b_limit + 100:
      del d[k]
    if float(v) <= b_limit - 50:
      del d[k]
  return d

#asks for a kcal limit, divides that limit arbitrarily, creates limits for all meals
print('What is your kcal limit for the day?')
limit = float(input())
b_limit = limit*0.30 
l_limit = limit*0.35
d_limit = limit*0.35

#sets the original dict to the limited dict
d_breakfast = limiter(d_breakfast)
d_burgers = limiter(d_burgers)
d_chicken = limiter(d_chicken)
d_salad = limiter(d_salad)

Traceback (most recent call last):
  File "/Users/********/Desktop/*****/setup.command", line 68, in     <module>
    d_breakfast = limiter(d_breakfast)
  File "/Users/*******/Desktop/*****/setup.command", line 54, in  limiter
    if float(v) >= b_limit + 100:
ValueError: could not convert string to float: 
logout

我想我为那里的某些线路找到了更好的解决方案,至少可以减少可能阻止转换的原因。现在,它非常明确地不会在似乎应该转换的位置转换:

def dictionizer(str1, str2): 
    str1 = str1.strip().split('\n') 
    str2 = str2.replace('\n', ',').split(',') 
    str2 = list(filter(None, str2)) 
    str2 = [float(i) for i in str2] d = dict(zip(str1, str2)) 
    return d 

仍然得到:      str2 = [str2中i的float(i)]      ValueError:无法将字符串转换为float:

为什么它将无法将str2中的所有内容转换为float?尝试整数,得到:     str2 = [str2中i的int(i)]     ValueError:以10为底的int()的无效文字:

1 个答案:

答案 0 :(得分:0)

尝试在if条件之前的限制器内打印v。在启用控制台的情况下转换为.exe文件。检查v值。

错误指出值v是一个字符串,无法转换为标志。

您的某些词典值不一致。