Carbon dioxide is produced by many organic processes and is a convenient volatile cue for insects1 that are searching for blood hosts2, flowers3, communal nests4, fruit5 and wildfires6. Although Drosophila melanogaster feed on yeast that produce CO2 and ethanol during fermentation, laboratory experiments7-12 suggest that walking flies avoid CO2. Here we resolve this paradox by showing that both flying and walking Drosophila find CO2 attractive, but only when they are in an active state associated with foraging. Their aversion to CO2 at low-activity levels may be an adaptation to avoid parasites that seek CO2, or to avoid succumbing to respiratory acidosis in the presence of high concentrations of CO2 that exist in nature13,14. In contrast to CO2, flies are attracted to ethanol in all behavioural states, and invest twice the time searching near ethanol compared to CO2. These behavioural differences reflect the fact that ethanol is a unique signature of yeast fermentation, whereas CO2 is generated by many natural processes. Using genetic tools, we determined that the evolutionarily conserved ionotropic co-receptor IR25a is required for CO2 attraction, and that the receptors necessary for CO2 avoidance are not involved in this attraction. Our study lays the foundation for future research to determine the neural circuits that underlie both state- and odorant-dependent decision-making in Drosophila.