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
What's wrong with this python code? (beginner's question) - Stack Overflow
What is wrong with this Python code? (Interview Question)
Storing an integer in a variable called
stris a bad idea in a weakly-typed language
While this is true that's not the problem here.
nbResults: str = 45;
this line stores an int value in a variable that's type hinted to be a str. Also semicolon is unnecessary.
More on reddit.comWhat's wrong with my code (python beginner)
What is wrong with my python code?
Videos
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.
I've been asked this question on an interview and I'll also share what I answered.
I would like to know if there's anything I haven't thought of?
Python code:
nbResults: str = 45;
My answer was the following:
-
Storing an integer in a variable called
stris a bad idea in a weakly-typed language -
stris a built-in function in Python, using that name like that hides the original function
>>> str(45) '45' >>> str=45 >>> str(45) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable
3. Syntax is obviously wrong
-
It could have been a function call like this:
nbResults(str = 45)
-
It could have been a function with an assignment, like this:
def nbResults(): str = 45;
Also a function with a default value for the argument, like this:
def nbResults(str = 45): ...
4. The value is wrong, it's 42, not 45. :)
Thanks for your help!
Storing an integer in a variable called
stris a bad idea in a weakly-typed language
While this is true that's not the problem here.
nbResults: str = 45;
this line stores an int value in a variable that's type hinted to be a str. Also semicolon is unnecessary.
Hello u/springuni, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:
-
Python code found in submission text but not encapsulated in a code block.
If I am correct then please follow these instructions to fix your code formatting. Thanks!
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)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.
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.
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