Exercise: Python

Questions for: Console Io

How can you read an integer from the console and check if it is within a specific range?
A:
check_range(input())
B:
number = int(input("Enter a number: ")); print("Within range" if 1 <= number <= 10 else "Out of range")
C:
range_check = lambda x: 1 <= x <= 10
D:
validate_range(input())
Answer: B
To read an integer from the console and check if it is within a specific range, use conditional statements with the int() function.
How can you align text to the center in a Python console output?
A:
align_center("text")
B:
print("text", align="center")
C:
print(f"{text:^10}")
D:
center_align("text")
Answer: C
Using the f-string format {text:^10} aligns the text to the center within a 10-character wide space.
How can you prompt the user for a yes/no response?
A:
response = input("Yes or no? (y/n): ")
B:
response = prompt_yes_no()
C:
response = get_response(["yes", "no"])
D:
response = input_yes_no()
Answer: A
To prompt the user for a yes/no response, use input("Yes or no? (y/n): ").
How can you read a line of text from the console and count the occurrences of each word?
A:
word_count(input())
B:
count_words(input())
C:
word_occurrences = Counter(input().split())
D:
input().count_words()
Answer: C
To read a line of text from the console and count the occurrences of each word, use Counter(input().split()) from the collections module.
How can you print a progress bar in the console using Python?
A:
print_progress_bar()
B:
progressbar.print()
C:
print("\u2588" * 10)
D:
console.print_progress()
Answer: C
To print a simple progress bar in the console, you can use the Unicode block character (\u2588) repeated a certain number of times.
Ad Slot (Above Pagination)
Quiz