Question

Write code which takes a sentence as an input from the user and then prints the length of the first word in that sentence. Hint #1: think about how you know where the first word ends - you may assume that the sentence contains more than one word. Hint #2: To make your life easier, use a string method that utilizes space. It looks kind of looks like this: String.method(" "). Sample run 1: Enter a sentence: Everything in its right place 10 Sample run 2: Enter a sentence: Down is the new up

Answers

There are no human answers yet.
Answered by GPT-4o mini
Here's a simple Python code that takes a sentence as input from the user and then prints the length of the first word in that sentence:

```python
# Prompt the user to enter a sentence
sentence = input("Enter a sentence: ")

# Split the sentence using space and get the first word
first_word = sentence.split(" ")[0]

# Calculate the length of the first word
length_of_first_word = len(first_word)

# Print the length of the first word
print(length_of_first_word)
```

### Explanation:

1. The code prompts the user to enter a sentence.
2. The `split(" ")` method is used to break the sentence into words at each space. This returns a list of words.
3. The first word is accessed with `sentence.split(" ")[0]`.
4. The `len()` function is used to calculate the length of the first word.
5. Finally, the length is printed.

### Sample Runs:
- If the user inputs `Everything in its right place`, the output will be `9`.
- If the user inputs `Down is the new up`, the output will be `4`.

Related Questions