Print 1 for Hello and 0 for Bye

EasyIf-Else Programs · 56 of 64

Write a Python program that prints 1 if the user enters Hello and 0 if the user enters Bye.

Advertisement
Input
Hello
Output
Enter your choice: Hello
1

Approach

  1. Compare the cleaned input with each word.

Solution

Python
choice = input("Enter your choice: ").strip().lower()if choice == "hello":    print(1)elif choice == "bye":    print(0)else:    print("Please type Hello or Bye")
Outputtyped input: Hello
Enter your choice: Hello
1

Line by line

Line 1

strip() removes stray spaces and lower() accepts any capitalisation.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Leave a Comment