Quick Python code question

Soldato
Joined
12 Feb 2009
Posts
4,359
Location
London
Im doing a Python course and im having trouble trying to understand what I have done wrong.

When an input of 4.7 which is in the range is entered it outputs 4.7.

Code:
# initialise the input_list with the given values
temperatures = (4.7)
# initialise the output_list to the empty list
faulty_fridge = []
# for each input_value of the input_list:
for temperature in temperatures:
    # if the input_value satisfies the condition:
    if temperature not in range(0,6):
        # append the input_value to the output_list
        faulty_fridge = faulty_fridge + [temperature]
# print the output_list
print(faulty_fridge)


My understanding is that the input is only added to the output list if it meets the 'if' criteria which it does not? Im sure there should be another way to do it rather than use a range but im a little stuck.

Something like this makes sense to me but Im getting a syntex error around the second condition of the 'if' so is obviously wrong.

Code:
if temperature <= 0 or => 5:
 
Last edited:
Soldato
OP
Joined
12 Feb 2009
Posts
4,359
Location
London
Never mind figured it out.

Code:
# initialise the input_list with the given values
temperatures = [-0.1, 0, 0.1, 1, 4, 4.6, 6, 7]
# initialise the output_list to the empty list
faulty_fridge = []
# for each input_value of the input_list:
for temperature in temperatures:
    # if the input_value satisfies the condition:
    if temperature < 0 or temperature > 5:
        # append the input_value to the output_list
        faulty_fridge = faulty_fridge + [temperature]
# print the output_list
print(faulty_fridge)
 
Man of Honour
Joined
19 Oct 2002
Posts
29,515
Location
Surrey
Does that even run? This line is declaring the variable temperatures to be a floating point number of 4.7. You then try to iterate over every temperature in the temperatures variable. But it's not possible to iterate over a floating point.

Code:
temperatures = (4.7)

EDIT: I see that you just posted a fix :)
 
Man of Honour
Joined
19 Oct 2002
Posts
29,515
Location
Surrey
OK. It's obviously better to use the code editor recommended by them during the course. But it's a fairly basic one. You might also want to take a look at Pycharm. There is a free version and it's excellent. It's made by the Jetbrains team who make great IDE's.

An IDE (Integrated Development Environment) adds a lot of extras. For example I copied your original code into Pycharm and it immediately highlighted that line as having a problem (it actually said the brackets were not necessary but that was an indication that the line was potentially incorrect). It also lets you set 'breakpoints' where you can debug your code and step through each line, looking at what the contents of the variables are, etc). Most importantly a code editor or IDE can highlight where there are issues and suggest what code you may want to type next (this is called 'code completion'). There are other IDE's and code editors as well and Pycharm is just one example.

I always recommend learning one thing at a time. As you're new to Python I'd suggest following the course with IDLE. But at some point when you feel more comfortable with Python itself I'd suggest taking a look at a proper code editor or IDE such as Pycharm.
 
Associate
Joined
6 Jun 2016
Posts
164
Location
Cambridge
I take your point that a beginner may find more comments helpful.

I would still urge .:SAmSuNG:. to review the comments and ask himself/herself if each one actually helps in explaining the code or are some of them just psuedocode that get in the way.

I don't think it is a bad idea to learn good practices from the outset.
 
Last edited:
Back
Top Bottom