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))
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))
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.