Python Forum
Teacher (thrown in at the deep end - help)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Teacher (thrown in at the deep end - help)
#1
Hi, I'm self learning Python and I need some help (desperately) - I understand that I am double accounting in my simple program (I am adding the total for above 1.57 and also adding for below 1.92). I've spent time attempting to write a nested if statement (if index >=1.57 and <= 1.92) but it isn't working.

Can someone on here help me please?? Oh I'm using version 3 I understand the syntax is different in earlier versions.

total = 0
all_heights = [1.87, 1.48, 1.57, 1.91, 2.01]
for index in all_heights:
    if index >=1.57:
        total = total +1
    if index <= 1.92:
        total = total +1
print ("The total number of qualifying astronauts is ",total)

time.sleep (2)
print ("It's not is it - the program is double counting the total")
Reply
#2
Welcome to Python and the forums!
First, please put your code in Python code tags and full error traceback message in error tags. You can find help here.
Reply
#3
if height >=1.57 and height <= 1.92:
        total = total +1
or

if 1.57 <= height <= 1.92:
        total = total +1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Sorry just noticed this now

will do from now on

Thanks for that - so I needed to refer to the index reference on both sides of the and statement - sorry for asking stupid questions and I appreciate your help (it won't be the last time I will need it) 8-)
Reply
#5
(May-22-2018, 12:35 PM)Mr90 Wrote: so I needed to refer to the index reference on both sides of the and
yes, that is right. or do it as my second example
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Just use the second example. This is more pythonic and better to read.

if min_val <= current_val <= max_val:
    pass
This allows also current_val == min_val or current_val == max_val.
If the min/max value should be outside of the range, use the < operator instead.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 826 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  deep learning python Stevedas 1 1,648 Sep-26-2021, 08:32 AM
Last Post: Yoriz
  Pytest API Post call thrown errors pyseeker 4 3,538 Dec-08-2019, 04:53 PM
Last Post: pyseeker
  Invalid argument error thrown. pyseeker 4 8,568 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,156 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 3,932 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  [split] Teacher (thrown in at the deep end - help) Mr90 2 2,964 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Parse XML - how to handle deep levels/hierarchy dwill 8 9,299 Apr-17-2018, 04:17 PM
Last Post: dwill
  Exception not thrown in python3 ONEoo7 7 6,054 Apr-09-2017, 01:58 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020