Python Programming Language-User Input
We have merely printed text onto the screen up until this point. However, we also have the option of entering our own data into the script. In this chapter, we'll examine user input and how to deal with it. Input Function We may retrieve user input from the console application using Python's input method. input = name ("Enter your name here:") print(name) The user's name is saved into the variable name when entered here. This variable can then be called and printed. number1 = input ("Enter first number: ") number2 = input ("Enter second number:") sum = number1 + number2 print("Result: ", sum) This illustration is a little deceptive. It appears that we are printing the sum after receiving two numbers as input. The function input always returns a string, which is a concern. As a result, when you enter 10, the variable's value is "10," which is a string. What occurs when we combine two strings, then? Simply put, we add one to the other. As a result, "15" and "26" would equal "1526" when added together. We must first typecast our variables if we wish to perform a mathematical addition. number1 = input("Enter first number: ") number2 = input("Enter second number: ") number1 = int(number1) number2 = int(number2) sum = number1 + number2 print("Result: ", sum) Our script is now effective. Always keep in mind that the input function returns a string, so if you wish to perform computations with it, you must typecast it.
0 Comments
Leave a Reply. |
AuthorAnything you need to know about computer science Archives
May 2023
Categories
All
|