In the case shown here
This is a good example on how we could use random() and multiply it by whatever number
In the case shown here
This is a good example on how we could use random() and multiply it by whatever number
ValueError
what are value errors?
you put in a value but the value is invalid (for example, maybe it's the wrong type)
function
use ctrl+F (find) to find the name errors since you probably just misspelled a name
Main Note: Parse errors are really just grammar errors Usually Python will tell you what line it's getting stuck on, so you look at that line and if it's not there then you look at the lines before The error messages are helpful because they give you hints Don't panic or be too rushed - trial and error and write stuff down to keep it all organized, which will eventually help you find the bug :)
One trick that can be very valuable in this situation is to simply start by commenting out the line number that is flagged as having the error.
# To see if the error is truly on that line, or before
TokenError: EOF in multi-line statement
TokenError: EOF in multi-line statement Means Python got to the end of file (EOF) while it was still looking for something
even if it says "error on line 4" it might not be on that line, you should look upwards because you probably need to do something before, and Python is getting stuck at line 4
SyntaxError:
Syntax Error: Means you made a grammar mistake!
In order to use a variable on the right hand side (the equation part), you have to define it first on the left hand side
Our brain tends to see what we think is there, so sometimes it is very hard to find the problem just by looking at the code. Especially when it is our own code and we are sure that we have done everything right!
It's not your fault if you do this - it's what everyone does! That's why error messages can help us
testing: it is important to test your code on a range of inputs.
Just because your code works for one input doesn't mean it'll work for all inputs, so you have to keep testing
What I learned here: order of operations changing variable type from string to integer typing a statement (print)
Question 4: starting_day = input("What day of the week does your vacation start on?")
length_of_stay = input("How many days are you on vacation?")
starting_day = int(starting_day)
length_of_stay = int(length_of_stay)
return_day = starting_day + length_of_stay % 7
print("You will return on day", return_day, ".")
What I did: input, turn to integers, divide by 7 and get the remainder
What I'm doing: Using the input function. Using the print function Using the type converter int to make the string an integer. Using the print function. Using the type function.
n = input("Please enter your name: ")
print("Hello", n)
age = input("How old are you?")
age = int(age)
print("You are", age, "years old.")
print(type(age))
The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another—if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits. Finally, returning to our time example, the remainder operator is extremely useful for doing conversions, say from seconds, to hours, minutes and seconds.
Ways the % operator can actually be useful
where you need floating point values, use the division operator /. If you want an integer result, use //.
/ vs //
/ to divide and get it as a float
// divide and get it as the int
% divide but only give us the remainder
python has some key words/reserved words that it reserves for their own use. ex: yield
= for assignment == for equality
type conversion functions: to change the object type from one to another. ex: from a string to a number
Triple quoted strings can even span multiple lines
value == object
data type == classes
"It usually takes longer to read a program because the structure is as important as the content and must be interpreted in smaller pieces for understanding."
Python can only execute a program if the program is syntactically correct
correct capitalization, correct order, etc. basically we need correct formatting
program
program "!= " algorithm
both are lists of step-by-step instructions, but a program is written in language computer can understand, while an algorithm isn't
"Programs often implement algorithms, but note that algorithms are typically less precise than programs and do not have to be written in a programming language."
conditional execution
<3
Interpreter: Read step 1, do step 1, read step 2, do step 2.
Compiler: Read all the steps, then do all the steps Source Code turns into Object Code aka Executable
Python is a high-level language. High-level languages are easier to write and are portable (can run on different kinds of computers) so we use high-level languages a lot more than low-level languages. High-level languages need to be run through an interpreter and compiler in order to be changed to a low-level language that a computer can understand (1's and 0's).
algorithms.
algorithms = the answers to problems you figure out when coding
it is a list of instructions. you follow them to solve the problem.