Lecture 9 Flashcards
Two methods to input data:
-prompt the user to input data while the program is running
-provide data at the program start unprompted
Input()
Is a built in python function that pauses the program and waits for the user to input some data. The user can can submit data by thping it into the terminal window and pressing enter on the keyboard
How is user input returned
It is returned as a string which may need to be converted to another tupe depending on how its used in the program
Input example:
name = input(“What is your name? ”)
name = name.split()
Splits name at the spaces, returns a list of all the strings between spaces
Comma_nums = input(“Provide 4 numbers separated by commas: “)
comma_num = comma_nums.split(“,”,3)
Splits the ‘comma_nums’ at the “,”. Does a max of 3 splits
back_to_back_nums = input(“Provide 4 numbers back to back: “)
back_to_back_nums =lust(back_to_back_nums)
Returns a list of all three characters in back to back nums
Trouble shooting sys.argv
If the python file is not in the same folder as your current directory you need to do one of two things:
1) change your current directory
2) include the path to the python file when trying to run the program
If the python is not on the path of your computer, include the path to python file when trying to run your program
Output
There are many ways to output data from a program(print to terminal screen print to file send to another program send to output
Here we will expand print to the terminal screen by learning how to properly format strings for print()
An f string is a formatted string that allows us to:
- embed variables and simple expressions directly into the string
-call functions within strings
-specify ways to display numeric data
Greeting= f”Hello World!”
Print(greeting)
Age= 19
Print(f”Dean os {age} years old.”)
Hello World
Dean is 19 years old.
Price = 25
Print(f”The price is {price:.2f}”)
The price is 25.00
Price = 25
Print(f”The price is {price:b}”)
11001
Price = 25
Print(f”The price is {price:x}”)
19
Price = 25
Print(f”The price is {10000000*price:5,.2f}”)
25,000,000.00
Price = 25
Print(f”The price is {1000000*price:15,.2f}”)
The price is 25,000,000.00
General syntax for float formatting: {value:{width}.{precision}}
- width is the minimum number of characters used to display the number
-precision is the number of characters used to display after the decimal
-Python will round a float when deciding what to display
def balance(amount, cost):
return amount-cost
Unit price = 25
Num_units = 3
Print(f”the total cost is {unit pricenum_units:.2f}”)
Print(f”The total cost is {‘Reasonable’ is unit_pricenumber_units <50 else ‘unreasonable’}”)
Print(f”You have {balance(100, unit_price*num_units):.2f} dollars remaining”)
The total cost is 75.00
The total cost is unreasonable
You have 25.00 dollars remaining
Print(f”|{‘col1’:<10}|{‘col2’:^10}|{‘col3’:>10}|”)
10 spaces to the right of column, 5 on either side of column 2, 10 to the left of column 3
\n
New line
\t
Tab character, moves to next tab every 8 characters
back_to_back_nums = input(“Provide 4 numbers back to back:”)
Back_to_back_nums = list(back_to_back_nums)
Returns a list of all the characters in back_to_back_nums