RESOURCES:
Class slides: https://docs.google.com/presentation/d/1IHtsWNQNQxU-tdgOF-WZ_byWW99wdQXqB9c0G-7W3l4/edit?usp=sharing
Example code (with comments): https://replit.com/@ShravyaS/IntroToPython-2
Recap:
- Separators: print('hi', 'hello', sep='^') outputs "hi^hello"
- Ending characters: print('this is a question', end='?') outputs "this is a question?"
- Math in Python: Either use the same datatype, or convert using int(), str(), float(), bool()
- Taking input: Input takes a value from the user. You can assign input to a variable with x = input()
HOMEWORK:
Write a program to take in input of a float and an integer: the float is a price for an item and the integer is how many of that item are ordered. Print out the total cost. (For example, buying 3 apples at $0.49 each means the total cost is $1.47.)
Post your answers as a comment, and let us know if you need help! We'll post solutions before next class.
Solutions:
cost = float(input("How much do apples cost?")) amount = int(input("How many apples do you want?")) print("$", cost*amount, sep="")
You can put a string inside the input() and it will print the string and you input the answer right after the string
print('what is the unit price?'); unitprice = float(input()); print('unit price is', unitprice); print('how many items?'); numitem = int(input()); print('number of item is', numitem); totalcost = unitprice*numitem; print('total cost is', totalcost)
Float:
string1 = "Each tomato is $5, Tomato's can also come in halves and quarters. Write how many Tomato's you're buying in a decimal" print(string1) T = float(input()) print(T * 5) print("That is how many dollars you'll pay for all the tomato's you bought") Integer:
string1 = ("Each Pear is $10, how many are you going to buy?") print(string1) P = int(input()) print(P * 10) print("That is how many dollars you will pay for the amount of pears you bought")
print('How much do the apples cost?') x = float(input()) print('How many do you want to buy?') y = float(input()) print('$',x*y, sep='')