Python Forum
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"if" beginner question
#1
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
Reply
#2
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

Reply
#3
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
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
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.
Reply
#7
WOW thank you so much guys, learned much in just one post, really thank you!!
Reply
#8
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.
Reply
#9
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
Reply
#10
x, y, and z are tuples. Instead of == use in to test for membership.
and please, use meaningful variable names, not cryptic one-letters
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,380 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,137 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,704 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,864 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 3,173 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,708 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 2,653 Aug-27-2019, 02:38 PM
Last Post: Bramen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020