从STDIN解析行时接收SyntaxError:EOF?

时间:2015-08-10 19:38:24

标签: python python-3.x

我正在尝试创建一个循环,当用户按下Enter时,它会退出循环并为其提供总计。我添加了一个try-except错误处理程序,使其可以正常工作,但必须有另一种方法可以防止用户输入空行时点击EOF

def main():
    cooling_deg_days = 0
    heating_deg_days = 0

while True:
    try:
        usr_in = eval(input('Please enter the average daily temperature in'
                            ' degrees Farenheit: '))
        if usr_in == '':
            break

        if usr_in > 80:
            deg_above = usr_in - 80
            cooling_deg_days += deg_above
            print('{} added to the CDD total.'.format(deg_above))
        elif usr_in < 60:
            deg_below = 60 - usr_in
            heating_deg_days += deg_below
            print('{} added to the HDD total.'.format(deg_below))
    except SyntaxError:
        break

print('Cooling Degree-Days total was {}.'.format(cooling_deg_days))
print('Heating Degree-Days total was {}.'.format(heating_deg_days))


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

检查输入是否为mSearchView.QueryTextChange += (s, e) => { queryText = e.NewText; GetUserList(queryText, PageIndex); }; private void GetUserList(string queryData, int PageIndex) { UserRepository.UserRepositoryClient client = new UserRepository.UserRepositoryClient(Config.Binding.BasicHttpBinding(), new EndpointAddress(Config.ServiceReferences.Repositories.UserRepositoryUrl) ); using (var scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageHeaders.Add(Global.Header.GetHeaderModern()); client.QueryAsync(new UserRepository.UserQueryArgument { FullNameLastFirst = queryData, Pager = new UserRepository.QueryPager { Index = PageIndex, Size = 30, ZeroIndexed = true } }); client.QueryCompleted += client_QueryCompleted; } } void client_QueryCompleted(object sender, UserRepository.QueryCompletedEventArgs e) { if (e.Result.Data != null ) { RunOnUiThread(() => UpdateView(e.Result.Data.ToList())); } else { RunOnUiThread(() => UpdateView(null)); } } } 的数字,然后使用isdigit()代替int()

eval()