Hollow square function with two inputs? - python

I've been struggling to create a function that can put out a default hollow square of 5x5 but then also take in 2 inputs.
Here's the question:
create a charsqr.py function that when imported will result in the following:
Program:
def main():
charsqr()
charsqr(4)
charsqr(3,'#')
main()
Result:
*****
* *
* *
* *
*****
****
* *
* *
****
###
# #
###
Here's my code:
def charsqr(chars,sym):
if type(chars) = int and type(sym)=str:
print(str(sym)*chars)
for i in range(chars-2):
print(str(sym)+" "*(chars-2)+str(sym))
print(str(sym)*chars)
else:
print("*"*5)
for i in range(5):
print("*"+" "*3+"*")
print("*"*5)
I've been fiddling around with the if statement as I've been getting "missing 2 positional arguments" error. I know how to create a hollow square, just not sure how to get the program to print the default square and then a square with the given "chars" without needing a "sym".
Any feedback is appreciated ! :)

You're complicating this way too much, a simple:
def draw_square(size=5, edge="*", fill=" "):
hollow = size - 2 # just so we don't calculate the hollow part each time
print(edge * size) # top part
for _ in range(hollow): # iterate until the last line
print(edge + fill * hollow + edge) # middle part
print(edge * size) # bottom part
Should do the trick. You can even change the 'hollow' part to some character.

You apparently want a function that uses an asterisk if no character is stated, and uses size 5 if no size is stated. That is what default parameters are for in Python:
def charsqr(chars=5, sym='*'):
Then calling charsqr() is the same as charsqr(5, '*'), and calling charsqr(4) is the same as charsqr(4, '*'). Calling charsqr(3, '#') uses no defaults.
Note that the code you show has bad indentation and will give a syntax error. Indent the lines of your function (everything but the def line). Also replace the = symbols in your if line with ==. With those three changes, your code works and gives the desired output.

Related

Beginner Python surface calculation of a hut

I'm trying to make a program to calculate the surface area of ​​a shack with a pitched roof. I've only been in this class for 2 weeks and I'm a bit overwhelmed. The program should ask the user via console for the values ​​and then calculate the values ​​using the definition.
I'm not asking for the entire code at all. But I don't understand how I can calculate inputs using a definition and then print them. this is my code so far:
import math
def floorspace(a,b):
G = 0
G = a*b
return (G)
#main program
a = int(input("enter the length of the floor!"))
b = int(input("Enter the width of the floor!"))
print(floorspace, G)
You don't need to import math as basic multiplication is already included. You also don't need to initialize a variable before you assign it so I removed the
G = 0
G = a*b
lines and replaced it with a simple
return a*b
You don't need brackets around a return statement, just a print statement.
The final two issues are that you're printing incorrectly and you used the wrong function parameters. You would need to pass in the same number of parameters that are in the function declaration (so in this case, 2). Pass in a and b from the user inputs into your floorspace() function and then call print(). The code should work now!
def floorspace(a,b):
return a*b
#main program
a = int(input("enter the length of the floor!"))
b = int(input("Enter the width of the floor!"))
print(floorspace(a,b))
in your code print(floorspace,G) G is not defined you must write your it like this print(floorspace(a,b))

unresolved reference during calling a function

I keep getting "Unresolved Reference" error when, I'm trying to call function
here is code for area of circle
#area of circle
def arc(a,r):
a=3.14 * (radius**2)
return arc
radius=int(input("Enter Radius: "))
area_of_circle=arc(a,r)
print("Area of circle: {}".format(arc))
There are a few errors in here, I'll step through each and explain what's happening.
The first two issues occur in your arc() function. I'll write comments above each offending line:
def arc(a,r):
# What is `radius`? That's not defined in this function!
a=3.14 * (radius**2)
# You're returning a reference to the arc() function from the arc() function?
return arc
The next two issues are in the main area of your program. Again, I'll write comments above the offending lines:
radius=int(input("Enter Radius: "))
# What is `a`? What is `r`? You haven't defined those names!
area_of_circle=arc(a,r)
# `arc` is a function, so what do you expect to happen here?
print("Area of circle: {}".format(arc))
To clean this up, you need to make sure every name references a valid variable, and that you're using functions and variables correctly. In your particular program, functions should be defined and called, not returned from functions or passed around like variables in .format() statements. Variables should be defined and given a valid value.
There are a few other errors that may arise once you clean those issues up, but I'm hoping you'll be able to solve those once you have most of the issues fixed!
Fixed up the variables/names/labels in your code:
# area of circle
def area_of_circle(radius):
area = 3.14 * (radius ** 2)
return area
radius = int(input("Enter Radius: "))
area_of_circle = area_of_circle(radius)
print("Area of circle: {}".format(area_of_circle))

when i run my code, why does the ellipse/circle not show up

from math import sin
from processing import *
X = 30
Y = 30
delay = 16
radius = 30
def setup():
strokeWeight(10)
frameRate(20)
size(500,500)
def sircle():
global X, Y, radius
background(100)
fill(0,121,184)
stroke(255)
fc = environment.frameCount
X += (mouse.x-X)/delay;
Y += (mouse.y-Y)/delay;
radius = radius + sin(fc / 4)
draw = sircle
run()
for some reason run() only creates the background.
does anybody know how to call the function for sircle()?
I think the OP is referring this code
where it does seem correct that draw is being assigned the function variable sircle. Besides, its not like sircle() is returning anything that can be assigned to draw
Looking at the example code in the link I shared above, you need a line like
ellipse(X,Y,radius,radius)
at the end of your sircle function
You need to run sircle() and setup() with parentheses.
These are functions not variables, they demand their parentheses. In your code draw variable stores the memory address of sircle() function.

Initializing and displaying grid - python

I'm attempting to create a game similar to battleship, and I'm having trouble figuring out how I would initialize the board by having each cell begin with an 'O' and displaying it to the user. A requirement for the function player_board() is that it's supposed to take in a grid representing the player's game board as an argument and output it to the user's screen. This is a portion of my code that I'm struggling with. I'm also not sure why it keeps printing out an extra 'O' at the end. Any help or feedback would be appreciated!
import random
sizeof_grid = 9
chance = 10
def mines():
grid = [{("M" if random.randint(0, chance) == 0 else " ") for i in
range(sizeof_grid)} for i in range(sizeof_grid)]
return grid
def initialize_board():
start_board=[["O" for i in range(sizeof_grid)] for i in range(sizeof_grid)]
return start_board
def players_board():
for r in initialize_board():
for c in r:
print (c, end="")
print()
return c
print(players_board())
You get the extra "O: because of the last line of code. You call the function with print(players_board) and in the function itself you return c (which has the value of one "O"). This means you print out the return value of the function which is "O".
You can execute the function with players_board() and remove the print().
Also you can remove the return c at the bottom of the function.

I am trying to make a cosine law calculator in python

I am trying to write a program that takes side "a" and side "b" and angle "C". Then it will output side "c".
I am getting an int error and I don't know whats wrong
Here is my code:
import math
def triangle():
a=input("Enter side a:")
b=input("Enter side b:")
angle=input("What is the angle:")
side=(a**2)+(b**2)
rest=(2*(a*b))(math.cos(angle))
done=side-rest
end=math.sqrt(done)
print end
triangle()
It might because you missed a "*" in:
rest=(2*(a*b))(math.cos(angle))
it should be:
rest=(2*(a*b)) * (math.cos(angle))
Your problem is this line:
rest=(2*(a*b))(math.cos(angle))
You are missing a * operator:
rest=(2*(a*b))*(math.cos(angle))
You've also got some excessive use of parenthesis:
rest = 2 * a * b * math.cos(angle)
The cause of the problem is that python thought you were trying to invoke the result of the expression (2*(a*b)). However that is an int, and it is not a callable object.

Categories