0% found this document useful (0 votes)
21 views3 pages

Python For Beginners - A Practic - Daniel Correa - Part32

Uploaded by

Kabir West
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Python For Beginners - A Practic - Daniel Correa - Part32

Uploaded by

Kabir West
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
print("Enter your name: ") name = input() print("Your name is: ") on print(name) Iprint(“Enter your name: “ 2mame = input() 3 print("Your name is: ") 4 print (name) z Figure 6-1. Graphical representation of the Danial input function in the previous code. input function — default data type By default, whatever the user enters through the keyboard after calling the input function will be type str (text). It doesn’t matter if the input is a number or a decimal; it will still be defined as an str (text) type. The following code shows a program in which, on line 1, a person’s age is entered via keyboard. That age is stored in the variable age. Note that on line 1, we are sending an argument (a text) to the input function. This argument is optional, but when sent, the input function will print the value of the re- ceived argument (in this case, it will print “Enter your age:”). Then, line 2 will calculate half of the age received by dividing the variable age by 2 (and as- signing the result to the variable halfAge). Finally, on line 3, the program will print the value stored in the variable halfAge. However, as shown in Figure 6-2, the code will dis- play an error indicating that an str value (referring to the age variable) cannot be divided by an int value (referring to the number 2) because in Python, divid- ing a text by an integer is impossible. Additionally, we have confirmed that the input function always re- turns an str variable (regardless of whether we enter a number). Code and execute 1. age = input("Enter your age: ") 2. halfAge = age/2 3. print(halfAge) Enter your Typeerror Traceback (most rec Sipython- input-1-7a147fgeade3> in () 1 age = input ("Enter your age: ") ---> 2 halfiage = age/? 2 print(halfage) Typeerror: unsupported operand type(s) for /: ‘str’ and ‘int Figure 6-2. Execution of the previous code. To solve the previous problem, we need to perform a type conversion, which will be explained in the next section. TIP: Often, we spend a lot of time trying to make our code work without analyz- ing the obvious - “Read the damn error message” (quoted from: 2019 - Thomas, D., & Hunt, A. - The Pragmatic Program- mer: your journey to mastery). 6.2. Conversion of variables types The conversion of variable types is a common process in programming. Sometimes, we will receive information from the user as input that we need to convert to int or float data types. Other times, we may need to convert numbers to text to store infor-

You might also like