Exercise: Python

Questions for: Console Io

How can you print the factorial of a number entered by the user?
A:
print(factorial(int(input("Enter a number: "))))
B:
user_input = get_user_input(); print(calculate_factorial(user_input))
C:
num = input("Enter a number: "); print(fact(num))
D:
print("Factorial:", compute_fact(input()))
Answer: A
To print the factorial of a number entered by the user, use print(factorial(int(input("Enter a number: ")))).
How can you read a list of numbers from the console and find their average?
A:
average_numbers(input("Enter numbers separated by spaces: "))
B:
numbers = input("Enter numbers separated by spaces: "); print(sum(map(int, numbers.split())) / len(numbers))
C:
avg = calculate_average(input())
D:
print(avg_numbers(get_numbers()))
Answer: B
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)).
How can you prompt the user to enter a sentence and count the number of vowels?
A:
count_vowels(input("Enter a sentence: "))
B:
vowel_count(get_sentence())
C:
sentence = input("Enter a sentence: "); print(sum(1 for char in sentence if char.lower() in 'aeiou'))
D:
get_vowel_count("Enter a sentence: ")
Answer: C
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')).
How can you read a line of text from the console, reverse it, and print the result?
A:
print(reverse(input("Enter a line of text: ")))
B:
line = input("Enter a line of text: "); print(line.reverse())
C:
text = input("Enter a line of text: "); print(text[::-1])
D:
reversed_text = reverse_text(input())
Answer: C
To read a line of text from the console, reverse it, and print the result, use text = input("Enter a line of text: "); print(text[::-1]).
How can you read an integer from the console and print its square?
A:
square(int(input()))
B:
print_square(input("Enter an integer: "))
C:
num = input("Enter an integer: "); print(int(num) ** 2)
D:
calculate_square(int(input("Enter an integer: ")))
Answer: C
To read an integer from the console and print its square, use num = input("Enter an integer: "); print(int(num) ** 2).
Ad Slot (Above Pagination)
Quiz