Python 3.4.5 (default, Dec 11 2017, 16:57:19) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>At the prompt, type print('Hello, world!').
print('My name is : Fill your name') print('My age is: Fill your age'). print('My street number is: Fill your street number')Now run your code myinfo.py.
You will get an output
print('My age is: Fill your age'). ^ SyntaxError: invalid syntaxHint: What is the problem here? Where is the arrow pointing to?
After you solved the problem your output should be:
My name is : James Matthews My age is: 18 My street number is: 1300 South
Now lets try to use a prompt. Write a code were you request from a user (you at this point) to enter the information from the terminal. using the prompt will result in the code waiting for a response before it continues.
name = input("Enter your name:") age = input("Enter your age:") street = input("Enter your street number:") print('My name is', name) print('My age is',age). print('My street number is',street)Here in each case you have to write your response on a single line and hit Enter key to proceed. Now change the first line of the code from
name = input("Enter your name:")to
names = input("Enter your name:")
Run your code.
You must have gotten the NameError: name 'name' is not defined
Explain what happened?
Name = input("Enter your name:")Run your code.
Now change it to
NAME = input("Enter your name:")Run your code. Submit your error and explanation for all the variables (names, Name, NAME). Submit it in problem 0 in assignments. Call your file names.txt
a = "My name is: " b = 322 c = 51.1 d = 1 + 2jHere a,b, c, and d are defined as variables. We stored the values "My name" , 322, 51.0, 1+2j in these variables.
try to print out one or all of these variables?
try to print the variable C instead of c. Did that work?
you should understand from Exercise 0 why not.
Note that the variables are case sensitive.
There are also variables that are reserved for python. Built in names can not be used as variables for example: print, input. print and input are two functions you have used already in this class ro print variables and use the prompt to enter variables.
These "variables" we invented their names come in four data types:
string : a bunch of characters surrounded by single or double quotes. integer: a number with no decimal point float: a number with decimal point complex: a number written with j to indicated the imaginary partcreate another string (call it a1). assign a1 the value James. Use a single or double quotes. Note that strings can be connected using the "+" operator: Try the following:
b. print ( a + a1). c. print ( b + c ). d. print ( a + b ). c. print ( a + c ).Do all the previous prints work?
Here the operation d fails because you are adding a number to a string. You can fix this by in this case by converting the number to a string. You can do that by using the function str.
b1 = str(b) print (a + b1)Here instead of adding a ( a string) to b (an integer) [a failed operation that does not work in python] we converted b to a string in order for this operation to work. Python is quite particular about doing operations with the correct variable type.
Does it work now?
We just used str to converted a variable type from integer to string.
In the same manner the function int converts a variable to integer while float converts a variable to a float.
Now assume you have the following code:
b = 322 b1 = str(b) c = 51.1try to calculate b1 + c. Convert the correct variable using the correct function to get the proper value of the sum. Print out the sum and submit your code in problem 1. Call your code sum.py
>>> Tc = 40 >>> Tf = (9/5)*Tc + 32 >>> print (Tf) 104.0Now do the conversion for several centigrade temperature values (45, 62, -10).
#! /usr/plocal/bin/python3 Tc = 40 ratio = 9/5 constant = 32 Tf = ratio*Tc + constant print(Tf)Now do the conversion for several centigrade temperature values (45, 62, -10).