Exercise: Console Io
Questions for: Console Io
How can you prompt the user to enter a list of words, sort them alphabetically, and print the result?
A:
sorted_words(get_user_words())
B:
print("Sorted words:", sort_alphabetically(input("Enter words separated by spaces: ").split()))
C:
words = input("Enter words separated by spaces: ").split(); print("Sorted words:", sort_words(words))
D:
sort_input_words(input("Enter a list of words: "))
Answer: C
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)).
How can you read a string from the console, count the number of vowels and consonants, and print the result?
A:
vowel_consonant_count(input("Enter a string: "))
B:
print(count_vowels_consonants(get_user_string()))
C:
string_input = input("Enter a string: "); print("Vowels:", count_vowels(string_input), "Consonants:", count_consonants(string_input))
D:
print("Vowels:", vowels_count(input("Enter a string: ")), "Consonants:", consonants_count(input()))
Answer: C
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)).Discuss About this Question.
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?
A:
positive_sum(get_positive_numbers())
B:
sum_positive_numbers(input("Enter numbers (negative to stop): "))
C:
total = 0; while True: num = int(input("Enter a number: ")); if num < 0: break; total += num; print(total)
D:
print("Sum of positives:", calculate_positive_sum(get_input_numbers()))
Answer: C
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.Discuss About this Question.
How can you read a list of integers from the console, remove duplicates, and print the result?
A:
remove_duplicates(input("Enter integers separated by spaces: "))
B:
numbers = input("Enter integers separated by spaces: "); print(list(set(map(int, numbers.split()))))
C:
unique_values(get_user_input())
D:
print(remove_duplicates(get_numbers()))
Answer: B
To read a list of integers from the console, remove duplicates, and print the result, use
numbers = input("Enter integers separated by spaces: "); print(list(set(map(int, numbers.split())))).Discuss About this Question.
How can you prompt the user to enter three numbers and find the maximum among them?
A:
max_num(get_three_numbers())
B:
maximum = find_max(input("Enter three numbers separated by spaces: ")); print(maximum)
C:
max_value = input("Enter three numbers: "); print(find_maximum(max_value))
D:
print("Maximum:", max(input("Enter three numbers: ").split()))
Answer: D
To prompt the user to enter three numbers and find the maximum among them, use
print("Maximum:", max(input("Enter three numbers: ").split())).Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.