Posts: 25
Threads: 11
Joined: Jan 2019
Jan-12-2019, 06:58 AM
(This post was last modified: Jan-12-2019, 06:59 AM by Naito.)
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
Posts: 6
Threads: 0
Joined: Jan 2019
Jan-12-2019, 01:07 PM
(This post was last modified: Jan-12-2019, 01:07 PM by Mr_W.)
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
Save the input in a variable like this:
First_choice = input()
This will save the input in the variable First_choice. Do the same thing for the second choice. After that your if statement checks the possibilities but you should write it like this:
if First_choice == x and Second_choice == z:
print("????????????")
Posts: 31
Threads: 7
Joined: Jan 2019
Jan-12-2019, 05:08 PM
(This post was last modified: Jan-12-2019, 05:08 PM by Trinx.)
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!
Posts: 8,156
Threads: 160
Joined: Sep 2016
(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
Posts: 31
Threads: 7
Joined: Jan 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.
Posts: 31
Threads: 7
Joined: Jan 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.
Posts: 25
Threads: 11
Joined: Jan 2019
WOW thank you so much guys, learned much in just one post, really thank you!!
Posts: 1,950
Threads: 8
Joined: Jun 2018
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 25
Threads: 11
Joined: Jan 2019
Jan-13-2019, 12:51 PM
(This post was last modified: Jan-13-2019, 12:51 PM by Naito.)
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
Posts: 8,156
Threads: 160
Joined: Sep 2016
x, y, and z are tuples. Instead of == use in to test for membership.
and please, use meaningful variable names, not cryptic one-letters
|