Python Forum
My else clause doesn't work (general help)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My else clause doesn't work (general help)
#3
You really want the else statement on the for. That is, if the for loop does not find anything, you want to print there is no such day. The old way to do this uses a boolean variable:

day_found = False
for number, name in days:
    if num_inp == number:
        print(name)
        day_found = True
        break
if not day_found:
    print('There is no such day.')
Notice the break statement. Once we find the correct day (if there is one), there's no point in processing further. So we use the break statement to break out of the loop.

Python gives us a simpler way to do this, using that break statement. It allows us to put the else statement on the for loop. Then if the for loop finishes without a break statement, whatever code is in the else statement executes:

for number, name in days:
    if num_inp == number:
        print(name)
        break
else:
    print('There is no such day.')
This set of code works the same as the previous set of code, but you don't have to track a separate variable.

That solves the problem as you were looking at it, but the real problem is that you need a better data structure. A dictionary would be ideal here:

day_dict = dict(days)
if num_inp in day_dict:
    print(day_dict[num_inp])
else:
    print('There is no such day.')
The first line creates a dictionary where the keys are the first item in each tuple (the numbers) and the values are the second item in each tuple (the day names). The if statement checks to see if num_inp is one of the keys (and thus a valid day). If it is valid, the next line pulls the value from the dictionary using the key (num_inp). If you're not following this, there is a tutorial on dictionaries in the tutorials section.

And while it may not be ideal, you can also solve this problem with a list:

days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
print(days[num_inp])
Now you get an error raised for an invalid day. You can either handle that error, pass it on, or prevent it by checking that the day number is between 0 and 6 before printing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
My else clause doesn't work (general help) - by NMW - Jul-15-2017, 12:58 PM
RE: My else clause doesn't work (general help) - by ichabod801 - Jul-15-2017, 01:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PIP doesn't work YKR 1 824 Mar-28-2025, 02:10 PM
Last Post: snippsat
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 2,605 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Extending list doesn't work as expected mmhmjanssen 2 1,584 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 2,103 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,827 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 1,962 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  How to use the LIKE clause in Python Columbo 9 7,130 Oct-09-2022, 10:22 PM
Last Post: Larz60+
  client.get_all_tickers() Doesn't work gerald 2 2,814 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 7,272 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 3,186 Dec-18-2021, 02:38 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

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