0

So I've used python enough to know that this is really simple code and there isn't really a reason that it shouldn't be working. Essentially, I call an input and the user inputs a day of the week, I haven't assigned functions to each day yet, so I tested the input with an 'else' command which tells you "Invalid input", and then recalls the input and asks you again. I tested it by entering something other than what I have defined, and it returns nothing. It should print out "Invalid input." and then re-ask, but it doesn't.

Am I missing something really subtle, or does Python not work properly in Sublime Text 3?

Note: I am writing in Python3, but I never assigned Sublime Text 3 to use that version, I just assumed that it would already interpret it in the latest version of Python - maybe that's my mistake and if so; please can someone tell me how to fix it.

Here's the code:

def ask_day():
    day = input("What's the day today?: ")
    if day == monday:
        mon()
    elif day == tuesday:
        tue()
    elif day == wednesday:
        wed()
    elif day == thursday:
        thu()
    elif day == friday:
        fri()
    elif day == saturday:
        sat()
    elif day == sunday:
        sun()
    else:
        print("Invalid input. Enter a day of the week in lower-case.")
        ask_day()

def ask_time():
    time = input("Whats the current hour?: ")

def mon():
    pass


ask_day()

ask_time()

And here's the output:

What's the day today?: something else

Like I said, it should re-ask me to enter a day, buuut... nothing. It doesn't end, it just gets stuck.

Fun sidenote: Stop harassing this question, I only asked lol.

Matty Man
  • 23
  • 4
  • 1
    You need quotes around the days. – Red Jun 05 '20 at 01:44
  • 1
    The example you provide isn't a working one, what are `monday`, `tuesday` etc? – Grismar Jun 05 '20 at 01:44
  • 1
    @Grismar you can also say what are ``mon()``, ``tue()`` etc. – Red Jun 05 '20 at 01:46
  • 1
    to get good answers it's essential to provide the **shortest possible self contained example reproducing the issue**. We should be able to copy / paste it into a python file and run it to get the same error as you. – gelonida Jun 05 '20 at 01:47
  • @gelonida You should be able to copy paste it, that was everything I wrote, and everything it outputted. – Matty Man Jun 05 '20 at 01:52
  • @AnnZen Ah, thanks for that, just wanted a simple answer. – Matty Man Jun 05 '20 at 01:52
  • The main issue is, that your IDE (sublime text) as pointed out in the answer, is not handling input properly. I often noticed, that IDEs are hiding / creating problems. It's of course up to you, but what might make you more efficient is following. Use your IDE as usual **But** as soon as you have an issue, that you want to post on SO. Save the shortest reproducible example into a file (e.g. `problem.py`) and call from command line and post the snippet and the output. You should have seen immediately (after entering a week day) that `monday` is unknown. – gelonida Jun 05 '20 at 01:58
  • something else, that might be interesting is to use programs like pylint or flake8. They will complain a lot about coding style, but detect issues like uninitialized variables etc. example: `pip install flake8` and then `flake8 problem.py` look at https://pastebin.com/siWrAKqQ for an example It would show lines like: `problem.py:3:15: F821 undefined name 'monday'` or `problem.py:22:5: F841 local variable 'time' is assigned to but never used` – gelonida Jun 05 '20 at 02:03

1 Answers1

2

Problem

Sublime Text doesn't support Python's input function. (I know, it's quite annoying at first.) But it's a good thing! It forces you to learn other ways to run your code.

Solution

As coders gain experience, they begin to run their programs from the command line.

  • Step 1: Open your terminal
  • Step 2: Run python3 <your-file.py>. If you get an error like command not found, try python <your-file.py>.

Also

The words monday, tuesday, etc. should be surrounded by quotes as they are strings, not variables (you're not assigning values to them, they are the values themselves).

For example: 'monday'

I hope this helps! More info on running files from the command line here.

Thomas
  • 859
  • 6
  • 16
  • Hi dumb question - I enter '$ chmod +x Home/Desktop/SublimeText3/Python/Sched_Test.py' into the bash terminal, but I get an error. I think I'm doing it wrong, but I have no clue what's wrong. You seem like you probably know lol. It gives a "No such file or directory" error... (p.s sorry I'm a bit of a noob) – Matty Man Jun 05 '20 at 02:08
  • Try changing Home/Desktop/... to ~/Desktop/... – Thomas Jun 05 '20 at 02:10
  • Yeah that didn't solve it, but I managed to make it executable after I moved it straight to the Desktop. – Matty Man Jun 05 '20 at 02:22
  • Good! The issue was probably because the path was slightly off. If you find the file in Finder and click on it with two fingers, then press and hold the Option key, a useful feature called ‘Copy as Pathname’ pops up, which copies the path for you, no mistakes. :) – Thomas Jun 05 '20 at 02:28
  • im using a windows keyboard... I right clicked it and that didn't show up, but I'll just look it up online. Thanks for all ur help today – Matty Man Jun 05 '20 at 02:58
  • Making the script executable is only necessary if you intend to execute it directly (`./path/to/my/script.py` instead of `python3 ./path/to/my/script.py`) and this will stil require that you specify the program to use with a shebang at the top of the script. -- I wouldn't say it's not recommended, but it's clearly a lot of trouble for no added benefit in your case. – Romain Vincent Jun 05 '20 at 10:15
  • Absolutely. Thanks for the catch. I’ve changed it back to no chmod step. – Thomas Jun 05 '20 at 11:52