0
Could someone tell me what’s wrong with my code?
I’ve just started learning python and I’m trying to make a basic code here, but I can’t get the input to work? I use PyCharm on my schools computer and it works on that. https://code.sololearn.com/cTl73ECxzN6l/?ref=app
2 odpowiedzi
+ 9
On lines 2 and 5, you seem to have misused the input function. By setting input to equal a string in line 2, you mess with how the function is used and as a result raises on error whenever you try to use it again. Additionally, on line 5, you're calling the input function without actually using the value anywhere, so there would be no point to it. Just deleting those 2 lines allows your program to run better
In addition to the input functions, when you're actually taking in input and you want to convert it to an integer, just surround the input() function for the width and length variables in an int() function, which makes it so that you don't need to create lengthint and widthint (and because you're trying to use width and length as integers later on, that would raise an error as they are still strings at that point)
+ 2
Faisal has already mentioned some issues and optimizations in the code. There is an other issue by calculating the perimeter. See the code down here:
print('This program will find the area and perimeter of a rectangle')
#print('What is the length of your rectangle?')
length = int(input('What is the length of your rectangle: '))
#lengthint = int(length)
#print('What is the width of your rectangle')
width = int(input('What is the width of your rectangle: '))
#widthint = int(width)
#perimeterint = (length + width)/2 # calculation formula is not correct
perimeterint = (length + width) * 2
areaint = (length * width)
print('The perimeter of your rectangle is',perimeterint)
print('The area of your rectangle is', areaint)
# code after review:
print('This program will find the area and perimeter of a rectangle')
length = int(input('What is the length of your rectangle: '))
width = int(input('What is the width of your rectangle: '))
perimeterint = (length + width) * 2
areaint = (length * width)
print('The perimeter of your rectangle is',perimeterint)
print('The area of your rectangle is', areaint)