Python Forum
"if" beginner question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "if" beginner question (/thread-15302.html)

Pages: 1 2


RE: "if" beginner question - Mr_W - Jan-13-2019

I think because a can't be (for example) 'milk' and 'MILK' so to fix that problem you can write:
x="milk"
y="sugar"
z="tea"
t="coffee"
but ofcours the user can write it as Coffee or COFFEE so we have to rule out any upper case characters. You can modify a string by using .lower() or .upper() to change the whole string to upper or lower case. So to change the input to all lower case it would be a = input().lower().
For more explanation:
https://www.tutorialspoint.com/python/string_lower.htm

The full working code is in the spoiler but try it yourself first :)



RE: "if" beginner question - Naito - Jan-13-2019

(Jan-13-2019, 01:01 PM)Mr_W Wrote: I think because a can't be (for example) 'milk' and 'MILK' so to fix that problem you can write:
x="milk"
y="sugar"
z="tea"
t="coffee"
but ofcours the user can write it as Coffee or COFFEE so we have to rule out any upper case characters. You can modify a string by using .lower() or .upper() to change the whole string to upper or lower case. So to change the input to all lower case it would be a = input().lower().
For more explanation:
https://www.tutorialspoint.com/python/string_lower.htm

The full working code is in the spoiler but try it yourself first :)
thank you Mr_W that's so useful Big Grin
thank you buran


RE: "if" beginner question - metulburr - Jan-13-2019

also its common practice to use 4 spaces for indentation, not one. It much easier to identify.
Its a PEP 8 guideline.


RE: "if" beginner question - perfringo - Jan-13-2019

If names x, y, z, t are not used anywhere else it can be expressed in more readable form:

>>> first = input("Choose the first one: ").lower()
>>> second = input("Choose the second one: ").lower()
>>> if first == 'milk' and second == 'tea':
...     print("????????????")
... elif first == 'milk' and second == 'coffee':
...     print("typical")
... elif first == 'sugar' and b == 'tea':
...     print("okay")
... elif first == 'sugar' and second == 'coffee':
...     print("wake up")
... else:
...     print("is there any other comb?")