Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
71.
How can you prompt the user to enter a series of numbers until they enter a negative number, and then print the sum of the positive numbers?
positive_sum(get_positive_numbers())
sum_positive_numbers(input("Enter numbers (negative to stop): "))
total = 0; while True: num = int(input("Enter a number: ")); if num < 0: break; total += num; print(total)
print("Sum of positives:", calculate_positive_sum(get_input_numbers()))
Answer: Option
Explanation:
To prompt the user to enter a series of numbers until they enter a negative number, and then print the sum of the positive numbers, use a while loop with a break condition.

72.
How can you read a string from the console, count the number of vowels and consonants, and print the result?
vowel_consonant_count(input("Enter a string: "))
print(count_vowels_consonants(get_user_string()))
string_input = input("Enter a string: "); print("Vowels:", count_vowels(string_input), "Consonants:", count_consonants(string_input))
print("Vowels:", vowels_count(input("Enter a string: ")), "Consonants:", consonants_count(input()))
Answer: Option
Explanation:
To read a string from the console, count the number of vowels and consonants, and print the result, use string_input = input("Enter a string: "); print("Vowels:", count_vowels(string_input), "Consonants:", count_consonants(string_input)).

73.
How can you prompt the user to enter a list of words, sort them alphabetically, and print the result?
sorted_words(get_user_words())
print("Sorted words:", sort_alphabetically(input("Enter words separated by spaces: ").split()))
words = input("Enter words separated by spaces: ").split(); print("Sorted words:", sort_words(words))
sort_input_words(input("Enter a list of words: "))
Answer: Option
Explanation:
To prompt the user to enter a list of words, sort them alphabetically, and print the result, use words = input("Enter words separated by spaces: ").split(); print("Sorted words:", sort_words(words)).