Page 456 - ComputerScience_Class_11
P. 456

Program 1: Simple user input with names.

                   Program 1.py
                File  Edit  Format   Run   Options   Window    Help          Output

                name = input("Please enter your name: ")                  Please enter your name: Rohit
                print("Hello,", name)                                     Hello, Rohit


              Program 2: Write a program to ask the user's favourite colour.

                   Program 2.py

               File  Edit  Format    Run   Options   Window    Help

                colour = input("What is your favourite colour? ")
                print(colour + " is a beautiful colour!")




                   Output

                What is your favourite colour? Blue
                Blue is a beautiful colour!




              12.1.2 Getting Numeric Input
              When you use the input() function to enter numbers, Python treats the value as a string. It can not be used for
              mathematical calculations. If you try to add two numbers entered by the user, Python will join the strings together
              instead of performing actual addition.
              Program 3: The problem with treating numbers as strings.

                   Program 3.py
               File  Edit  Format    Run   Options   Window    Help

                eng = input("Enter marks in English: ")
                comp = input("Enter marks in Computer: ")
                print("Marks in English and Computer are : ", eng+comp)




                   Output
                Enter marks in English: 25
                Enter marks in Computer: 36
                Marks in English and Computer are :  2536



              To use numbers entered by the user, the input must be converted into a numeric data type. This can be done using the
              int() function for whole numbers or the float() function for decimal numbers.










                  454  Touchpad Computer Science (Ver. 3.0)-XI
   451   452   453   454   455   456   457   458   459   460   461