7
Contributors
11
Replies
75
Views
2 Years
Discussion Span
Last Post by HiHe
Featured Replies
  • 1

    Start here with your post [Click Here](http://www.daniweb.com/forums/post3625.html#post3625 ) Read More

  • 1

    python = 77777 if python: print("You picked a good language") Read More

  • In case you don't like multiple if/elif/else statements: ''' switch_case.py a switch/case like statement to replace multiple if/elif/else statements ''' # make input() work with Python2 and 3 try: input = raw_input except: pass def switch_case(case): return "You entered " + { '1': "one", '2': "two", '3': "three", '4': "four" … Read More

  • a = int(input("Enter a number: ")) # that is the function for entering an integer from keyboard if a == 0: print("It is 0") elif a < 0: print("It is negative") else: print("It is positive") #you can also use other operators if a > 5: print("a is bigger than 5") … Read More

  • Python allows conditional expression if y == 1: x = 5 else: x = 3 can written as x = 5 if y == 1 else 3 Read More

1

In case you don't like multiple if/elif/else statements:

''' switch_case.py
a switch/case like statement to replace
multiple if/elif/else statements
'''

# make input() work with Python2 and 3
try:
    input = raw_input
except:
    pass

def switch_case(case):
    return "You entered " + {
    '1': "one",
    '2': "two",
    '3': "three",
    '4': "four"
    }.get(case, "a bad number")

num = input("Input a number between 1 and 4: ")
print(switch_case(num))
1
a = int(input("Enter a number: ")) # that is the function for entering an integer from keyboard

if a == 0:
    print("It is 0")
elif a < 0:
    print("It is negative")
else:
    print("It is positive")

#you can also use other operators
if a > 5:
    print("a is bigger than 5")

if a < 11:
    print("a is smaller than 11")

if a > 5 & a < 100:
    print("a is bigger than 5 and smaller than 100")

if a > 50 | a > 100:
    print("a might be bigger than 50 or might be bigger than 100")

# and so n...
#hope that i was helping you

Edited by paladin952

1

Python allows conditional expression

if y == 1:
    x = 5
else:
    x = 3

can written as

x = 5 if y == 1 else 3
0

More examples:

# a multiconditional statement
if x > 0:
  if x < 10:
    print("x is larger than zero and less than 10")

# a simpler version
if 0 < x < 10:
  print("x is larger than zero and less than 10")

# is character lower case?
if 'a' <= character <= 'z':
  print(character)
0
# test string
mystr = "Zero Animal zones."

for c in mystr:
    if 'a' <= c <= 'z':
        print("character {} is between 'a' and 'z'".format(c))
    elif 'A' <= c <= 'Z':
        print("character {} is between 'A' and 'Z'".format(c))
    else:
        print(c, ord(c))
0
on = True
off = False
b1 = on
b2 = on
b3 = on

if b1 == b2 == b3:
    print("all buttons on")
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.