0

This is my first S.O. post!

I'm trying to learn Python with Murach's Python Programming- an excellent resource by the way...

Regarding the following code:

is_valid = True
customer_type = input('Enter customer type (r/w): ')
if customer_type == 'r' or customer_type == 'w':
    print(f'You entered: {customer_type}')
    # pass

else:
    print('Customer type must be "r" or "w".')
is_valid = False

I don't understand why the is valid True & False variables are required because the code appears to work fine when I comment them both out?

Thanks in advance!!

  • 2
    If that is the whole code they are redundant. – Yevhen Kuzmovych Mar 25 '21 at 16:03
  • 1
    It seems like the variable does nothing in this code. Please upload the full code or the reason why you added them to the code. – Roy Avidan Mar 25 '21 at 16:03
  • Agree, you dont need the `is_valid = ...` lines if that is all the code you have. – Selnay Mar 25 '21 at 16:04
  • This is likely not the full code. `is_valid` isn't used in any of the processes that function here. Without seeing the full code, we can only speculate what `is_valid` does. However, I assume that it is to check if the input is valid or not. Maybe it's just setting it up for something that is later to come in the course? – gmdev Mar 25 '21 at 16:04
  • It might make sense if `is_valid = False` is indented to be part of the `else` clause. – gofvonx Mar 25 '21 at 16:05
  • Thanks for all the quick responses! That is the entire code from the example in my book. – Sam Tomanio Mar 25 '21 at 16:08

3 Answers3

0

I think that the code should look like this:

is_valid = True
customer_type = input('Enter customer type (r/w): ')
if customer_type == 'r' or customer_type == 'w':
    print(f'You entered: {customer_type}')
    # pass

else:
    print('Customer type must be "r" or "w".')
    is_valid = False  # space here

Now is_valid value will show if you entered valid input or not. In this example it does not do anything. I think there may be other code related with this variable later in the book.

Somebody
  • 115
  • 6
0

This appears to be a code segment that determines whether the input is valid, prints an error message if it's not, and leaves that determination in is_valid for later code to use. A typical use is to repeat asking until the user makes a valid choice. In this case, you generally want the variable to be invalid, so that your loop condition "reads postivite":

invalid = True
while invalid:      # no extra operations in the condition
    # get input
    # validate input
Prune
  • 76,765
  • 14
  • 60
  • 81
-1

Yes, you are right this program will work completely fine without the is_valid variable. But to ensure that the user type either r or w we can use this is_valid variable using a while loop as follows

is_valid = True
while is_valid:
    customer_type = input('Enter customer type (r/w): ')
    if customer_type == 'r' or customer_type == 'w':
        print(f'You entered: {customer_type}')
    else:
        print('Customer type must be "r" or "w".')
        continue
    is_valid = False
Junaid
  • 159
  • 1
  • 15
  • You change `is_valid` after the loop. It is not going to finish – Yevhen Kuzmovych Mar 25 '21 at 16:12
  • sorry it was a typing error. I have adjusted it. Kindly take a look at it now – Junaid Mar 25 '21 at 16:15
  • Now it makes more sense. But not very intuitive as you are making `is_valid=False` when the input is actually correct. – Yevhen Kuzmovych Mar 25 '21 at 16:17
  • I guess in the question originally posted the is_valid variable does absolutely nothing. The only way it can be used in the code, if the input entered by the user is either r or w. Otherwise, it will ask for the user to enter a valid input and that actually serves the purpose. – Junaid Mar 25 '21 at 16:23
  • You can just initialize it to `False` and do a `while not is_valid`. And then make it `True` when the user's input **is valid**. – Yevhen Kuzmovych Mar 25 '21 at 16:32
  • Either way it is gonna ask the user for the correct input. The method you mentioned will return the same result as mine. I don't think there is any difference. – Junaid Mar 25 '21 at 16:41
  • I'm not saying there is a difference for the user. There is a difference for somebody who reads your code. – Yevhen Kuzmovych Mar 25 '21 at 16:57