Python Forum
"if" beginner question - Printable Version

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

Pages: 1 2


"if" beginner question - Naito - Jan-12-2019

Hello, I am a total beginner so can you help me please?
x="milk","Milk"
y="sugar","Sugar"
z="tea","Tea"
t="Coffee","Coffee"
print("please choose the combination you like")
print("milk", "", "sugar", "", "tea", "", "coffee")
print("chose the first one:")
input()
print("choose the second one:")
input()
if x and z:
 print("????????????")
elif x and t:
  print("typical")
elif y and z:
 print("okay")
elif y and t:
 print("wake up")
else:
 print("is there any other comb?")
i've made this code just to learn how "if" functions in python but the code just always sets the first boolean as true (even if you put whatever outputs) can you please help me? thank you
Output:
please choose the combination you like
milk sugar tea coffee
chose the first one:
cqf
choose the second one:
faef
????????????

Process finished with exit code 0


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

You should save the input in a variable. Now whatever you enter doesn't matter because your if statement is comparing x and z which are defined earlier. Not by the input. Check the link below. If that doesn't solve it check the spoiler
https://docs.python.org/3/tutorial/controlflow.html




RE: "if" beginner question - Trinx - Jan-12-2019

Try this:

print("Please choose the combination you like:")
print("milk", "", "sugar", "", "tea", "", "coffee")
print("Choose the first one:")
first = input()
print("Choose the second one:")
second = input()
if first.upper()[0] == 'M' and second.upper()[0] == 'S':
 print("????????????")
elif first.upper()[0] == 'M' and second.upper()[0] == 'T':
  print("typical")
elif first.upper()[0] == 'S' and second.upper()[0] == 'T':
 print("okay")
elif first.upper()[0] == 'S' and second.upper()[0] == 'C':
 print("wake up")
else:
 print("Is there any other combination?")
I used a command .upper which converts the input to all caps. Then I used [0] which means that the input is now only the first letter and it's a capital letter. So if the user said coffee, python would read that as C. You could also use the 'or' command if you want.

elif first.upper()[0] == 'S' or 'T' and second.upper()[0] == 'T' or 'S':
 print("okay")
Like that. I hope this helps you, I know it helped me explaining this. I'm not that great of a programmer either but I'm learning! Wink


RE: "if" beginner question - buran - Jan-12-2019

(Jan-12-2019, 05:06 PM)Trinx Wrote: elif first.upper()[0] == 'S' or 'T' and second.upper()[0] == 'T' or 'S':
Please, read Multiple expressions with "or" keyword


RE: "if" beginner question - Trinx - Jan-12-2019

(Jan-12-2019, 05:10 PM)buran Wrote:
(Jan-12-2019, 05:06 PM)Trinx Wrote: elif first.upper()[0] == 'S' or 'T' and second.upper()[0] == 'T' or 'S':
Please, read Multiple expressions with "or" keyword

Cool, thanks for pointing that out to me. That was something I had been wondering how to do for awhile now.


RE: "if" beginner question - Trinx - Jan-12-2019

Naito, here's an update to the 'or' command. So instead of doing:

elif first.upper()[0] == 'S' or 'T' and second.upper()[0] == 'T' or 'S':
 print("okay")
It should be:

elif first.upper()[0] in ('S', 'T') and second.upper()[0] in ('T', 'S'):
 print("okay")
Instead of 'or', use 'in'. You can make a longer list of varying letters.


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

WOW thank you so much guys, learned much in just one post, really thank you!!


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

Some observations:

input() is more readable and concise when used with argument:

>>> first = input('Choose the first one: ')
Choose the first one: 
In order to get spaces between choices you don't need to enter empty strings. Same result can be achieved by adding space at the end of string or writing it in one string with double spaces:

>>> print("milk", "", "sugar", "", "tea", "", "coffee")
milk  sugar  tea  coffee
>>> print('milk ', 'sugar ', 'tea ', 'coffee')
milk  sugar  tea  coffee
>>> print('milk  sugar  tea  coffee')
milk  sugar  tea  coffee
 



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

thank you guys for your suggestions
i am just woundering why this code dosen't work:
x="milk","Milk","MILK"
y="sugar","Sugar","SUGAR"
z="tea","Tea","TEA"
t="Coffee","Coffee","COFFEE"
print("please choose the combination you like")
print("milk sugar tea coffee")
a = input("chose the first one")
b = input("choose the second one:")
if a == x and b == z:
 print("????????????")
elif a == x and b == t:
 print("typical")
elif a == y and b == z:
 print("okay")
elif a == y and b == t:
 print("wake up")
else:
 print("is there any other comb?")
output:
please choose the combination you like
milk sugar tea coffee
chose the first onemilk
choose the second one:coffee
is there any other comb?

Process finished with exit code 0
iam trying to learn by making some mistakes while coding thanks guys


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

x, y, and z are tuples. Instead of == use in to test for membership.
and please, use meaningful variable names, not cryptic one-letters