Working with DeepSeek in Python
James Chapman
Curriculum Manager, DataCamp
A train travels 60 miles in 1.5 hours. If it continues at the same speed, how far will it travel in 4 hours, and what time will it arrive if it leaves at 2:15 PM?
A train travels 60 miles in 1.5 hours. If it continues at the same speed, how far will it travel in 4 hours, and what time will it arrive if it leaves at 2:15 PM?
Calculate the speed (60 ÷ 1.5 = 40 mph)
Multiply by 4 hours to find the distance (40 × 4 = 160 miles)
Add 4 hours to the departure time (2:15 PM + 4 hours = 6:15 PM)
Return: "160 miles, arrival at 6:15 PM"
response = client.chat.completions.create(
# On DeepSeek's API: model="deepseek-reasoner" model="deepseek-ai/DeepSeek-R1",
messages=[ {"role": "user", "content": train_problem}] ) print(response.choices[0].message.content)
<think>
Let's solve the problem step by step.
### **1. Calculate the Speed of the Train**
First, determine the speed of the train using the given distance and time.
\[
\text{Speed} = \frac{\text{Distance}}{\text{Time}} = \frac{60 \text{ miles}}{1.5 \text{ hours}} = 40 \text{ mph}
\]
### **2. Determine the Distance Traveled in 4 Hours**
Using the speed calculated above, find out how far the train will travel in 4 hours[...]
### **3. Calculate the Arrival Time**
If the train leaves at **2:15 PM** and travels for **4 hours**, the arrival time is[...]
</think>
### **Final Answers**
- **Distance traveled in 4 hours:** \(\boxed{160 \text{ miles}}\)
- **Arrival time:** \(\boxed{6:15 \text{ PM}}\)
response_content = response.choices[0].message.content
import re match = re.search(r'<think>(.*?)</think>', response_content, re.DOTALL)
think_content = match.group(1).strip() print(think_content)
Let's solve the problem step by step.
### **1. Calculate the Speed of the Train**
First, determine the speed of the train using the given distance and time.
\[
\text{Speed} = \frac{\text{Distance}}{\text{Time}} = \frac{60 \text{ miles}}{1.5 \text{ hours}} = 40 \text{ mph}
\]
### **2. Determine the Distance Traveled in 4 Hours**
Using the speed calculated above, find out how far the train will travel in 4 hours[...]
### **3. Calculate the Arrival Time**
If the train leaves at **2:15 PM** and travels for **4 hours**, the arrival time is[...]
Working with DeepSeek in Python