Code Example (Chained if else statement)

Problem 1: Ask the user to key in two numbers. Compare the two variables and display one of the following result:

  • number 1 is bigger
  • number 2 is bigger
  • They are equal

Code:

x = input("Enter number 1: ")
y = input("Enter number 2: ")
x = float(x)
y = float(y)
if x > y:
    print("number 1 is bigger")
elif x < y:
    print("number 2 is bigger")
else:
    print("They are equal")


Problem 2: Your friend just caught a pokemon. He told you its name, but not its combat power (CP). Given the picture, write code to find out the CP of his pokemon.


Code:

name = input("Enter the name of pokemon: ")
if name == 'Spinarak':
    print("CP is 43")
elif name == 'Wooper':
    print("CP is 38")
elif name == 'Qwilfish':
    print("CP is 24")
elif name == 'Sentret':
    print("CP is 20")
elif name == 'Rattata':
    print("CP is 10")
elif name == 'Charmander':
    print("CP is 10")
else:
    print("Name not found!")


Last modified: Wednesday, 8 May 2019, 9:09 PM