In [ ]:
*** input() ***
In [ ]:
We can use the command input() to define variables. The basic structure of the input() function is:
variablename = input("text")
"text" to the user should always be in quotes. Any information entered by the user will be stored as a string even if
it is a number. To get numbers from the user, we should use the structure below:
variablename = eval(input("text"))
The eval() function converts the text entered by the user into a number.
In [1]:
name = input("Enter your name: ")
print("Hello ", name)
Hello John
In [7]:
year = input("What is your birth year? ")
print("You are ", 2024 - year, "years old.") # the value stored in year is a string, not a number
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[7], line 2 1 year = input("What is your birth year? ") ----> 2 print("You are ", 2024 - year, "years old.") # the value stored in year is a string, not a number TypeError: unsupported operand type(s) for -: 'int' and 'str'
In [8]:
year = eval(input("What is your birth year? "))
print("You are ", 2024 - year, "years old." ) # now the value stored in year is a number, not a string
You are 59 years old.
In [ ]:
Here are some more examples:
Ask the user to enter a number. Then print out the square of that number.
In [11]:
num = eval(input("Enter a number: "))
print("The square of", num, "is", num * num, ".")
The square of 7 is 49 .
In [ ]:
The print() function puts a space between each arguments inside the function. That is why we have a space between
49 and the period (.). If you do not want this space, then you have to use another argument called sep.
The argument sep will change the spaces between arguments to anything else. For example, sep="" will not insert any space
between the arguments. In this case, you should insert spaces inside the quotes wherever you want to in order to make the
printout look better.
In [12]:
num = eval(input("Enter a number: "))
print("The square of", num, "is", num * num, ".", sep="")
The square of7is49.
In [16]:
num = eval(input("Enter a number: "))
print("The square of ", num, " is ", num * num, ".", sep="")
The square of 7 is 49.
In [ ]:
Ask the user to enter a number x. Then print out x, 2x, 3x, 4x and 5x, each seperated by three dashes.
In [18]:
x = eval(input("Enter a number: "))
print(x, 2*x, 3*x, 4*x, 5*x)
print(x, 2*x, 3*x, 4*x, 5*x, sep="---")
4 8 12 16 20 4---8---12---16---20
In [ ]:
Write a program that asks the user for a weight in kilograms and converts it to pounds.
There are 2.2 pounds in a kilogram.
In [19]:
weight = eval(input("Enter a weight in kilograms: "))
print(weight, "kilogram(s) is(are) ", 2.2*weight, "pounds.")
4 kilogram(s) is(are) 8.8 pounds.
In [ ]:
Write a program that asks the user to enter three numbers (use three separate input statements).
Create variables called total and average that hold the sum and average of the three numbers and
print out the values of total and average.
In [21]:
num1 = eval(input("Enter the first number: "))
num2 = eval(input("Enter the second number: "))
num3 = eval(input("Enter the third number: "))
total = num1 + num2 + num3
average = total / 3
print("The total of the three numbers (", num1, ", ", num2, ", ", num3, ") is ", total, ".", sep="")
print("The average is ", average, ".", sep="")
The total of the three numbers (3, 4, 5) is 12. The average is 4.0.
In [ ]: