in Python, Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line. Show Answer & Explanation <p>s a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet</p> Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet Unlock full access to Course Hero Explore over 16 million step-by-step answers from our library Subscribe to view answer gue
gue Step-by-step explanation e ve
gue cing elit. Nam lacinia
You can use:
Both Quick sys.stdin examples compatible with Python 2 and 3, Windows, UnixYou just need to
We can see that
file exampleSay you have a file,
Longer answerHere's a complete, easily replicable demo, using two methods, the builtin function, To begin with, let's create a file for inputs:
And using the code we've already seen, we can check that we've created the file:
Here's the help on
Builtin function, input (raw_input in Python 2)The builtin function Thus, here's how you can use
And let's print it back out to ensure it's as we expect:
Again, And when And on Linux/Unix, we can pipe from cat:
Or we can just redirect the file from stdin:
We can also execute the module as a script:
Here's the help on the builtin
|