I am new to coding and need some help

Question:
I am old and have no computer knowledge. I just retired from the military and am going to school to be a vet. I have to learn code for an elective to get my piece of paper to apply to vet school. This code is kicking my but.
This code is supposed to help determine temp of body after death for first 4 hours. the body temperature of 37 C at death and 27 C ambient
Im not sure what I am doing wrong.
It is telling me it won’t float and the numbers are off?

temperature_0 = float(37) # Initial body temperature in Celsius
temperature_1 = temperature_0 + (27 - temperature_0) * 0.2
temperature_2 = temperature_1 + (27 - temperature_1) * 0.2
temperature_3 = temperature_2 + (27 - temperature_2) * 0.2
temperature_4 = temperature_3 + (27 - temperature_3) * 0.2
print("Temperature 1 hour after death:
{:.1f}°C".format(temperature_1)) print("Temperature 2 hours after
death: {:.1f}°C".format(temperature_2)) print("Temperature 3 hours
after death: {:.1f}°C".format(temperature_3)) print("Temperature 4
hours after death: {:.1f}°C".format(temperature_4))
1 Like

Hey @DarylSager, welcome to the community!

In normal strings, you can’t have new lines. So you should convert your strings to ones with triple quotes. And also, you need to have a new line before the next print statement. You also don’t need to set the initial body temperature to a float. So here is your updated code

temperature_0 = 37 # Initial body temperature in Celsius
temperature_1 = temperature_0 + (27 - temperature_0) * 0.2
temperature_2 = temperature_1 + (27 - temperature_1) * 0.2
temperature_3 = temperature_2 + (27 - temperature_2) * 0.2
temperature_4 = temperature_3 + (27 - temperature_3) * 0.2
print("""Temperature 1 hour after death:
{:.1f}°C""".format(temperature_1))
print("""Temperature 2 hours after
death: {:.1f}°C""".format(temperature_2))
print("""Temperature 3 hours
after death: {:.1f}°C""".format(temperature_3))
print("""Temperature 4
hours after death: {:.1f}°C""".format(temperature_4))

Hope this helped!

6 Likes

Thank you.
This is the first assignment in the class and I am lost. I really appreciate the help.

This is what it tells me when I paste that stuff into the R studio.

mean_length
+ print("""Temperature 4
Error: unexpected string constant in:
"mean_length
print(""""

Oh, you’re programming in R? The topic specifies Python, but you just mentioned RStudio.

Well, in that case, you will want to use the following code instead. I have no idea how to use R, and I just did some quick googling, so I’m not 100% sure if this will work.

It also looks like variables are sometimes to be declared with <-, but I don’t know for sure. I believe you can also use round() instead of format().

Let me know if you need an explanation on anything, I’ll try my best to clear things up to the best of my knowledge.

temperature_0 <- 37 # Initial body temperature in Celsius
temperature_1 <- temperature_0 + (27 - temperature_0) * 0.2
temperature_2 <- temperature_1 + (27 - temperature_1) * 0.2
temperature_3 <- temperature_2 + (27 - temperature_2) * 0.2
temperature_4 <- temperature_3 + (27 - temperature_3) * 0.2

cat("Temperature 1 hour after death:\n", round(temperature_1, 1), "°C\n")
cat("Temperature 2 hours after death:\n", round(temperature_2, 1), "°C\n")
cat("Temperature 3 hours after death:\n", round(temperature_3, 1), "°C\n")
cat("Temperature 4 hours after death:\n", round(temperature_4, 1), "°C\n")
3 Likes

Ahh, yeah, that’s my bad, I kinda just assumed Python (since your code was very similar to Python). bobastley’s answer should have what you want.

2 Likes

It tells me the below

cat("Temperature 4 hours after death:\n", round(temperature_4, 1), "°C\n")
Error: object 'temperature_4' not found

Seems to work fine for me after testing. Did you accidentally remove a line or something?

2 Likes

I just cut and pasted it into r studio top left box and hit run. Then the following shows up in the bottom left box.

cat("Temperature 4 hours after death:\n", round(temperature_4, 1), "°C\n")
Error: object 'temperature_4' not found
1 Like

Weird, can you post a screenshot of RStudio?

usually, it is control p, right? When I do that It do

sorry had to look up you tube how-to screen shot. I was a machine gunner so not much computer training there

1 Like

2 Likes

Ok, you haven’t saved the script. Go to the top and click/hover on the File button:

There should be an option to save.

Alternatively, you can press Ctrl + S

Note I’ve never used RStudio before

5 Likes

you could re-write this code with a loop, making it more maintainable:

temperature = 37
for(hours_since in 1:4){
  temperature = temperature + (27 - temperature) / 5
  cat(
    "Temperature ",
    hours_since,
    "h after death: ",
    round(temperature, 1),
    "°C
",
    sep="",
  )
}
5 Likes

I appreciate it. I have office hours on zoom so hopefully I get some insight.