Exercise: Console Io
Questions for: Console Io
How can you read a sentence from the console, find the length of each word, and print the result?
A:
word_lengths(input("Enter a sentence: "))
B:
print_lengths(get_sentence())
C:
sentence = input("Enter a sentence: "); print([len(word) for word in sentence.split()])
D:
lengths = find_word_lengths(get_input_sentence())
Answer: C
To read a sentence from the console, find the length of each word, and print the result, use
sentence = input("Enter a sentence: "); print([len(word) for word in sentence.split()]).
How can you check if a given year is a leap year or not?
A:
is_leap(int(input("Enter a year: ")))
B:
check_leap_year(get_year())
C:
year = input("Enter a year: "); print(is_leap_year(year))
D:
leap = leap_year(input("Enter a year: "))
Answer: A
To check if a given year is a leap year or not, use
is_leap(int(input("Enter a year: "))).Discuss About this Question.
How can you read a string from the console, replace all spaces with underscores, and print the result?
A:
print(replace_spaces(input("Enter a string: ")))
B:
string_input = input("Enter a string: "); print(string_input.replace(" ", "_"))
C:
replace_chars(get_user_string())
D:
print(replace_whitespace(get_input()))
Answer: B
To read a string from the console, replace all spaces with underscores, and print the result, use
string_input = input("Enter a string: "); print(string_input.replace(" ", "_")).Discuss About this Question.
How can you print the ASCII value of a character entered by the user?
A:
ascii_value = ord(input("Enter a character: ")); print(ascii_value)
B:
char = get_char_input(); print(ascii(char))
C:
print(ord(input("Enter a character: ")))
D:
ascii_code(get_input_char())
Answer: A
To print the ASCII value of a character entered by the user, use
ascii_value = ord(input("Enter a character: ")); print(ascii_value).Discuss About this Question.
How can you read two integers from the console and swap their values?
A:
swap(int(input()), int(input()))
B:
a, b = input("Enter two numbers: ").split(); a, b = b, a; print(a, b)
C:
swap_values(get_integers())
D:
a = input("Enter the first number: "); b = input("Enter the second number: "); swap(a, b)
Answer: B
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).Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.