Your problem is that you have put your code into functions, but never call them.
When you define a function:
def shape():
...
To run that code, you then need to call the function:
shape()
Note that Python runs code in order - so you need to define the function before you call it.
Also note that to call a function you always need the brackets, even if you are not passing any arguments, so:
if shape == 1: rectangle
Will do nothing. You want rectangle().
Your problem is that you have put your code into functions, but never call them.
When you define a function:
def shape():
...
To run that code, you then need to call the function:
shape()
Note that Python runs code in order - so you need to define the function before you call it.
Also note that to call a function you always need the brackets, even if you are not passing any arguments, so:
if shape == 1: rectangle
Will do nothing. You want rectangle().
A better coding style will be putting everything inside functions:
def display():
print('This program will tell you the area some shapes')
print('You can choose between...')
print('1. rectangle')
print('or')
print('2. triangle')
def shape():
shap = int(input('What shape do you choose?'))
if shap == 1: rectangle()
elif shap == 2: triangle()
else:
print('ERROR: select either rectangle or triangle')
shape()
def rectangle():
l = int(input('What is the length?'))
w = int(input('What is the width?'))
areaR=l*w
print('The are is...')
print(areaR)
def triangle():
b = int(input('What is the base?'))
h = int(input('What is the height?'))
first=b*h
areaT=.5*first
print('The area is...')
print(areaT)
if __name__=="__main__":
display() #cal display to execute it
shape() #cal shape to execute it
You're appending to your list as you iterate over it, so every time you take a "step forward", you add another "step" to take later, so you're ending up with ['a', 'b', 'c', 'd', 'ax', 'bx', 'cx', 'dx', 'axx', 'bxx' ...]. For a whole host of reasons similar to this, a general rule is you should avoid modifying a list as you iterate over it.
Try this instead
list_1 = ['a', 'b', 'c', 'd']
list_2 = [elem + 'x' for elem in list_1]
result = list_1 + list_2
print(result)
list = ['a', 'b', 'c', 'd']
list2 = []
for element in list:
list2.append(element + "x")
list.extend(list2)
print(list)
Since you were appending inside the loop you used to get the memory error. The above code might help you.
What's wrong with my code (python beginner)
Correcting my code
Errors with my python code - Stack Overflow
What is wrong with this Python code? - Stack Overflow
Videos
I started coding about two weeks ago and began writing some code to take in user input in the form of a letter grade and return a value, but when I tried out the code in terminal it just printed out <function grade_value at 0x7fa6831d1f70>. I would like for it to return the value of the grade.
# values for letter grades
A = 20
B = 17.5
C = 15
D = 12.5
E = 10
F = 0
# user grade
grade = input('your grade: ')
grade = str(grade)
# returns user grade value as int
def grade_value(user_grade):
if user_grade == 'A':
return A
elif user_grade == 'B':
return B
elif user_grade == 'C':
return C
elif user_grade == 'D':
return D
elif user_grade == 'E':
return E
elif user_grade == 'F':
return F
else:
return('error')
# call function and print value (just testing)
grade_value(grade)
print(grade_value)You need to indent your code. Python doesn't like it when you do this for example:
def main():
print "Hello World!"
Python wants indents (I think it's 4 spaces)
def main():
print "Hello World!"
What line number is it where you have syntax and indent errors? by the way?
Python uses indentation for defining blocks instead of {}.
Also you should keep using the first indentation formating during all document. It means if you start with tabs you have to always use tabs even if spaces look the same visually and if you are using 4 space you can't change later in the code for tabs or more space.
The error you mentioned usually occurs because you pasted code from the internet and it is using tabs and you are using spaces or vice versa.
I recommend to re-indend the whole code as it's short.
In case you're using Python 3, the print statement is gone in that version and you need to use the print() function.
See: http://docs.python.org/release/3.0.1/whatsnew/3.0.html#print-is-a-function
You're using python 3.
use
print("blah")
The print statement turned into the print function in the transition.
With so many nested if statements, you're bound to run into this kind of issue. Here's a slightly more expandable solution:
Copyoptions = {
'L': (25, 3, 1),
'M': (20, 3, 1),
'S': (15, 2, 1),
}
prices = options[size]
price = prices[0]
if add_pepperoni == 'Y':
price += prices[1]
if extra_cheese == 'Y':
price += prices[2]
Looking at the logic of your code, I can say that you can add cheese on a pizza only if you already have some pepperoni.
Copyif L:
...
if P:
...
if C:
...
if M:
...
if P:
...
if C:
...
if S:
...
if P:
...
if C:
...
Is not like :
Copyif L:
...
if P:
...
if C:
...
if M:
...
if P:
...
if C:
...
if S:
...
if P:
...
if C:
...
Note that it may not be the better design for this problem, but as begineers, we have to start somewhere and improve :-)
Have a nice journey in coding and discover algorithms logic !
If c isn't the biggest, you always return c. That includes when it's the smallest.
To fix it? Well, I'd just do
Copyreturn sorted([a, b, c])[1]
but since this looks like homework, that answer probably relies too much on the library and too little on your own critical thinking. Instead, what if you found the biggest input, then returned the bigger of the other two?
Yes, I know this is your homework but I couldn't resist. This is one method that does not use sorting (and it works I believe).
Copydef median(a, b, c):
if (a >= b and b >= c) or (c >= b and b >= a):
return b
elif (a >= c and c >= b) or (b >= c and c >= a):
return c
else:
return a
In the lines where you do
guesses - 1
you decrement the value of guesses by 1 but you are not assigning the result to a variable. What you need to do is:
guesses = guesses - 1
There is also a shorter way to write the same thing:
guesses -= 1
Variable is being decreased by 1 but it is not saving the new value. So, as @BioGeek answer, you have to update your variable.