-6

I have this program:

print()
print ('------MENU------')
print ('1. Welcome to Python')
print ('2. Python is fun')
print ('3. This could be a challenge')
print ('4. Exit')
print()
choice = int(input('please enter a choice between 1 to 4: '))
for choice in (1,5):
      if choice ==1:
            print ('Welcome to python')
      elif choice == 2:
            print ('Python is fun')
      elif choice == 3:
            print ('This could be a challenge')
      else:
            break

It should print the MENU first and then asks to input an integer. My question is why it prints twice each time I input an integer between 1 to 3?

bestmbaman
  • 11
  • 5

1 Answers1

5

With for choice in (1,5): you tell your program: "Do the following once for choice = 1 and once for choice = 5. Here, the for is a for-loop, it does not mean "for the case that...".

What you probably meant was if choice in range(1, 5). The range is also important, otherwise you will just test whether choice is in the tuple (1, 5), i.e. either 1 or 5. Alternatively, you can also do if 1 <= choice < 5.

(Note: After changing the for to if, you will probably get a problem with the break, as that's just allowed in loops. Alternatively, you could use return if that code is in a function, or just exit() to exit the program, or nothing at all, if it's the last statement in the program anyway.)

tobias_k
  • 74,298
  • 11
  • 102
  • 155
  • I must say I dislike seeing `range()` in the context of range checks. Even more so when it's suggested to beginners. The latter suggestion of using `if 1 <= choice < 5` is a very clear pattern that continues to work for `float`s. Getting used to using `range()` for range checks can easily lead to confusion. – Fynn Becker Jan 28 '19 at 21:14
  • 1
    @FynnBecker I agree, that's why I added the comparison chaining alternative, but the `in range(...)` is probably what OP had in mind when writing the code. Also, at least in Python 3, and, of course, for `int`, testing `in range(...)` is actually [not that bad](https://stackoverflow.com/q/30081275/1639625). – tobias_k Jan 28 '19 at 21:24
  • @FynnBecker, Can you explain *why* in Python3 `if in range(1, 6)` is *worse* than `if 1 <= choice < 5`? `3.0 in range(1, 6)` still works, of course, and you have a free, implicit check you aren't allowing a fractional input. – jpp Jan 28 '19 at 21:43
  • @jpp I'm aware that as with almost everything there are viable use-cases for it, I don't see any for this concrete problem though. I addressed this because the OP is obviously new to python and recommending `range()` to beginners left a sour taste because of a certain popular book. As for using `range()` to implicitly ensure `int`s, I'm not sure about it. I'd probably still accompany it with a comment in my code to point that out because I don't consider it to be expressive enough (as with most implicit things). – Fynn Becker Jan 28 '19 at 22:20
  • Thanks very much guys, you are so right I am absolutely a beginner and this was the reason I was struggling with this code! Would appreciate if this forum is not for beginners then what should a beginner like me do who is ambitious to learn this language flawlessly and quickly, if I don't experiment then I won't learn and if I don't ask for pro help then what? – bestmbaman Jan 29 '19 at 22:09