Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.
Login
Login
OnlineGDB Q&A
Questions
Unanswered
Tags
Ask a Question
Ask a Question
How do I make this line of code shorter?
–1
vote
asked
Oct 31, 2018
by
Ender
(
120
points)
if x == "hello":
print("great")
elif x == "hi":
print("great")
I want to make the if hi and if hello shorter into if hi and hello
python
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
4 Answers
0
votes
answered
Oct 31, 2018
by
Kevin Sweny
(
190
points)
If you wanted the logic to be if x == hi, OR hello
You would use if ((x == "hello") || (x x == "hi")), the operator for OR is ||
commented
Dec 11, 2018
by
Tomas Z
(
180
points)
operator || is not in python, but you can use or keyword instead.
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
+2
votes
answered
Nov 3, 2018
by
Prabhat Mishra
if(x=='hello' or x=='hi'):
print("great")
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
+4
votes
answered
Jan 4, 2019
by
Raphael SCHMITZ-DU MONT
(
220
points)
another way would be
if x in ['hello', 'hi']:
print('great')
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
0
votes
answered
Jun 13, 2021
by
ITSME
(
760
points)
You can use or operator
if (x == "hi" or "hello"):
print ("Great")
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...