Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
51.
How can you print a countdown from 5 to 1?
countdown(5, 1)
for num in reversed(range(1, 6)): print(num)
print_countdown(5)
print(5, 4, 3, 2, 1)
Answer: Option
Explanation:
To print a countdown from 5 to 1, use a loop with reversed(range(1, 6)).

52.
How can you read a string from the console and check if it contains the substring "python"?
contains_python(input())
check_substring(input(), "python")
input().has_substring("python")
print("Python" in input())
Answer: Option
Explanation:
To read a string from the console and check if it contains the substring "python", use the in keyword.

53.
How can you print the sum of even numbers from 1 to 10?
print(sum(range(2, 11, 2)))
even_sum(1, 10)
print(even_numbers_sum(1, 10))
sum_even(range(1, 11))
Answer: Option
Explanation:
To print the sum of even numbers from 1 to 10, use sum(range(2, 11, 2)).

54.
How can you prompt the user to enter a floating-point number and round it to the nearest integer?
float(input("Enter a number: ")).round()
round(float(input("Enter a number: ")))
int(input("Enter a number: "))
round_int(input("Enter a number: "))
Answer: Option
Explanation:
To prompt the user to enter a floating-point number and round it to the nearest integer, use round(float(input("Enter a number: "))).

55.
How can you read a line of text from the console, remove leading and trailing whitespaces, and convert it to uppercase?
text = input().trim().upper()
text = input().strip().uppercase()
text = input().strip().upper()
text = input().trim().uppercase()
Answer: Option
Explanation:
To read a line of text from the console, remove leading and trailing whitespaces, and convert it to uppercase, use input().strip().upper().