Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
46.
How can you prompt the user for a yes/no response?
response = input("Yes or no? (y/n): ")
response = prompt_yes_no()
response = get_response(["yes", "no"])
response = input_yes_no()
Answer: Option
Explanation:
To prompt the user for a yes/no response, use input("Yes or no? (y/n): ").

47.
How can you align text to the center in a Python console output?
align_center("text")
print("text", align="center")
print(f"{text:^10}")
center_align("text")
Answer: Option
Explanation:
Using the f-string format {text:^10} aligns the text to the center within a 10-character wide space.

48.
How can you read an integer from the console and check if it is within a specific range?
check_range(input())
number = int(input("Enter a number: ")); print("Within range" if 1 <= number <= 10 else "Out of range")
range_check = lambda x: 1 <= x <= 10
validate_range(input())
Answer: Option
Explanation:
To read an integer from the console and check if it is within a specific range, use conditional statements with the int() function.

49.
How can you print the square of numbers from 1 to 5?
for num in range(1, 6): print(num * num)
print_squares([1, 2, 3, 4, 5])
squares_print(1, 5)
print(range(1, 6) ** 2)
Answer: Option
Explanation:
To print the square of numbers from 1 to 5, use a loop with range(1, 6) and calculate the square with num * num.

50.
How can you read a list of comma-separated values from the console and convert them to integers?
input().split(',').to_int()
list(map(int, input().split(',')))
read_comma_separated_integers()
convert_to_int(input().split(','))
Answer: Option
Explanation:
To read a list of comma-separated values from the console and convert them to integers, use list(map(int, input().split(','))).