Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
61.
How can you prompt the user to enter a sentence and count the number of vowels?
count_vowels(input("Enter a sentence: "))
vowel_count(get_sentence())
sentence = input("Enter a sentence: "); print(sum(1 for char in sentence if char.lower() in 'aeiou'))
get_vowel_count("Enter a sentence: ")
Answer: Option
Explanation:
To prompt the user to enter a sentence and count the number of vowels, use sentence = input("Enter a sentence: "); print(sum(1 for char in sentence if char.lower() in 'aeiou')).

62.
How can you read a list of numbers from the console and find their average?
average_numbers(input("Enter numbers separated by spaces: "))
numbers = input("Enter numbers separated by spaces: "); print(sum(map(int, numbers.split())) / len(numbers))
avg = calculate_average(input())
print(avg_numbers(get_numbers()))
Answer: Option
Explanation:
To read a list of numbers from the console and find their average, use numbers = input("Enter numbers separated by spaces: "); print(sum(map(int, numbers.split())) / len(numbers)).

63.
How can you print the factorial of a number entered by the user?
print(factorial(int(input("Enter a number: "))))
user_input = get_user_input(); print(calculate_factorial(user_input))
num = input("Enter a number: "); print(fact(num))
print("Factorial:", compute_fact(input()))
Answer: Option
Explanation:
To print the factorial of a number entered by the user, use print(factorial(int(input("Enter a number: ")))).

64.
How can you read two integers from the console and swap their values?
swap(int(input()), int(input()))
a, b = input("Enter two numbers: ").split(); a, b = b, a; print(a, b)
swap_values(get_integers())
a = input("Enter the first number: "); b = input("Enter the second number: "); swap(a, b)
Answer: Option
Explanation:
To read two integers from the console and swap their values, use a, b = input("Enter two numbers: ").split(); a, b = b, a; print(a, b).

65.
How can you print the ASCII value of a character entered by the user?
ascii_value = ord(input("Enter a character: ")); print(ascii_value)
char = get_char_input(); print(ascii(char))
print(ord(input("Enter a character: ")))
ascii_code(get_input_char())
Answer: Option
Explanation:
To print the ASCII value of a character entered by the user, use ascii_value = ord(input("Enter a character: ")); print(ascii_value).