57
Sep 28 '18
Can you genericize the code so it can take arguments for either cassettes, diskettes floppy drives, VHS tapes and SNES cartridges?
66
u/flobbley Sep 28 '18 edited Sep 28 '18
generalized for cassette, VHS, floppy disks, and SNES Cartridges:
cassette = [' ______________ ','| __ __ |','| / \ / \ |','| __/ __/ |','| __________ |','|_/_O______O__|'] VHS = [' ______________________ ','| |','| ____________ |','| / | | \ |','| | | | | |','| __|______|__/ |','||____________________||','|______________________|'] floppy = [' _____________ ','|| | __ | | ','|| | | || | ','|| | |__|| | ','||___|______| | ','| ___________ | ','|| | | ','|| | | ','|| | | ','||___________|_| '] snes = [' _____________ ',' __|| ||__ ','|__|| ||__| ','|__||___________||__| ','|__| ___________ |__| ','|__|| ||__| ','|__||___________||__| '] def printSquare(side, obj, length): for n in range(side): for part in obj: print(part * side) return "" class triangle: def __init__(self,side,obj,length): self.side = side self.obj = obj self.length = length def upperleft(side, obj, length): row = side for n in range(side): for part in obj: print(part * row) row-=1 return "" def upperright(side, obj, length): row = int(side) for a in range(0,row): for part in obj: parts = row - a space = ' '*length print(space * a,end="") print(part * parts) return "" def bottomright(side, obj, length): row = int(side) for a in range(row,0,-1): for part in obj: parts = row+1 - a space = ' '*length print(space * int(a-1),end="") print(part * parts) return "" def bottomleft(side, obj, length): row = side for n in range(0,side): for part in obj: print(part*int(n+1)) return "" def printCassettes(): more = True while more: objects = [cassette, VHS, floppy, snes] obj = objects[int(input('What are you looking for?\n1. Cassettes\n2. VHS Tapes\n3. floppy disks\n4. SNES Cartridges\n'))-1] square = False length = len(obj[1]) while not square: size = int(input("how many do you want?\n")) sizes = [1,2**2,3**2,4**2,5**2,6**2,7**2] if size in sizes: square = True else: print('That\'s not square!') side = int(size**0.5) print('What shape?') print('1. Rectangle') print('2. Triangle') condition = int(input())-1 if condition != 0: print('Where do you want them?') print('1. Upper Right') print('2. Upper Left') print('3. Bottom Right') print('4. Bottom left') condition = int(input()) if condition == 0: print(printSquare(side, obj, length)) elif condition == 1: print(triangle.upperright(side, obj, length)) elif condition == 2: print(triangle.upperleft(side, obj, length)) elif condition == 3: print(triangle.bottomright(side, obj, length)) elif condition == 4: print(triangle.bottomleft(side, obj, length)) if condition != 0: print('Yeah I know I didn\'t give you all of them, that\'s what you get for asking for a triangle') print('Want more?\n 1. Yes\n 2. No') wantMore = int(input()) if wantMore == 2: break return "" print(printCassettes())9
3
u/darez00 Nov 26 '18
You were so preoccupied with whether or not you could that you didn't stop to think if you should
0
27
14
u/flobbley Sep 28 '18
code:
cassette = [' ______________ ','| __ __ |','| / \ / \ |','| __/ __/ |','| __________ |','|_/_O______O__|']
def printSquare(side):
for n in range(side):
for part in cassette:
print(part * side)
return ""
class triangle:
def __init__(self,side):
self.size = side
def upperleft(side):
row = side
for n in range(side):
for part in cassette:
print(part * row)
row-=1
return ""
def upperright(side):
row = int(side)
for a in range(0,row):
for part in cassette:
parts = row - a
print(' ' * a,end="")
print(part * parts)
return ""
def bottomright(side):
row = int(side)
for a in range(row,0,-1):
for part in cassette:
parts = row+1 - a
print(' ' * int(a-1),end="")
print(part * parts)
return ""
def bottomleft(side):
row = side
for n in range(0,side):
for part in cassette:
print(part*int(n+1))
return ""
def printCassettes():
more = True
while more:
square = False
while not square:
size = int(input("how many cassettes do you want?\n"))
sizes = [1,2**2,3**2,4**2,5**2,6**2,7**2]
if size in sizes:
square = True
else:
print('That\'s not square!')
side = int(size**0.5)
print('What shape?')
print('1. Rectangle')
print('2. Triangle')
condition = int(input())-1
if condition != 0:
print('Where do you want them?')
print('1. Upper Right')
print('2. Upper Left')
print('3. Bottom Right')
print('4. Bottom left')
condition = int(input())
if condition == 0:
print(printSquare(side))
elif condition == 1:
print(triangle.upperright(side))
elif condition == 2:
print(triangle.upperleft(side))
elif condition == 3:
print(triangle.bottomright(side))
elif condition == 4:
print(triangle.bottomleft(side))
if condition != 0:
print('Yeah I know I didn\'t give you all of them, that\'s what you get for asking for a triangle')
print('Want more?\n 1. Yes\n 2. No')
wantMore = int(input())
if wantMore == 2:
break
return ""
print(printCassettes()) 19
u/marakpa Sep 28 '18
You know what? We could grab a .csv with lots of casettes from the 80’s (like, the Billboard top 200s from 1980 to 1990) and generate a casette with a random album name in it.
I just don’t know how to keep the space proportion so it doesn’t break... I’ll make it myself later if I have the time.
Also it should have the casette’s brand like Panasonic or TDK.
Edit: your “bored” program will end up being a new Spotify competitor by tomorrow morning at this pace.
8
15
u/Mr_Education Sep 28 '18
I want more.
6
53
u/roguemystic Sep 28 '18
As someone who is just getting started with learning Python this is actually quite the educational post. A working python script, a clear visual output, and a host of good comments on how to make it better! Thanks yall!
66
28
u/Coocos Sep 28 '18
Want more?
40
u/5erif φ=(1+ψ)/2 Sep 28 '18
_______________ | __ __ | | / \ / \ | | __/ __/ | | ___________ | |_/_o_______o__|40
Sep 28 '18
We need a casette bot
13
u/King_Joffreys_Tits Sep 28 '18
Could be OPs new project to learn more about python
0
Sep 28 '18
[deleted]
11
u/flobbley Sep 28 '18
I would love to do that, but I have no idea how to use python to interact with websites yet. I'll get on it though.
15
u/hansolo669 Sep 28 '18
For Reddit look up PRAW ... definitely the best way to interact with Reddit from python
18
16
u/mh3f Sep 28 '18
Code golf is fun. 27 lines to 15. Could condense a few more lines but I've been neglecting work to do this :)
from math import sqrt
cassette = (' ______________ ', ' | __ __ |', r' | / \ / \ |', r' | __/ __/ |', ' | __________ |', r' |_/_o______o__|')
while True:
count = int(input("How many cassettes do you want?\n"))
columns = sqrt(count)
if columns.is_integer():
for _ in range(0, count, int(columns)):
for line in cassette:
for _ in range(int(columns)):
print(line, end="")
print()
else:
print("That's not a square!")
if input("Want more? 1) Yes, 2) No\n") == "2":
break12
u/notafuckingcakewalk Sep 28 '18 edited Sep 28 '18
Gonna eschew fewer lines for readability and functionality:
def get_factors(count): root = count ** 0.5 if root.is_integer(): return int(root), int(root) for factor in range(int(root), 1, -1): other = count / factor if other.is_integer(): return int(factor), int(other) raise ValueError("{} is a prime number".format(count)) cassette = [ ' ______________ ', ' | __ __ |', ' | / \ / \ |', ' | __/ __/ |', ' | __________ |', ' |_/_o______o__|' ] def draw(count): cols, rows = get_factors(count) for _ in range(rows): for line in cassette: print("{}".format(line * cols)) def main(): while True: count = int(input("How many cassettes do you want?\n")) try: draw(count) except ValueError as err: print("Enter a valid number: {}".format(err)) continue if (input("Want more? [Y]/N ") or "Y").strip().upper() != "Y": break2
u/Farranor Sep 30 '18
print("{}".format(line * cols))is equivalent toprint(line * cols), andprint("Enter a valid number: {}".format(err))is equivalent toprint("Enter a valid number:", err).
5
4
u/Python4fun Java4work Sep 28 '18
I would use a generator statement sizes = [x**2 for x in range(10)]
4
u/renscoguy Sep 29 '18
But that's a list comprehension, a generator would be:
sizes = (x**2 for x in range(10))That being said, either would work just as well for this case. Even better than that would be:
If not (count**0.5).is_integer(): #not a square
4
u/talldstag [::-1] Sep 29 '18
As other comments will say, there are better ways to write this, but who honestly cares if your having fun? Screw the end user. Don't tell them it requires square numbers. Don't tell them you only allow 1-7 squared. That's part of the adventure of programming
1
1
u/pycepticus from pprint import pprint as print Sep 28 '18
GitHub link?
2
u/flobbley Sep 28 '18
I don't know what this means
1
u/pycepticus from pprint import pprint as print Sep 28 '18
Put your code on GitHub so others may easily enjoy your cassette creation program :)
1
3
u/fried_green_baloney Sep 28 '18
Hi, this is Jeff in the Denver Sales Office. We have a major contract that requires non-square numbers. So if you could get that done by 3 PM for the demo, that'd be great.
More seriously, that's kinda cool.
2
2
u/philintheblanks Sep 28 '18
If you want to take this brand of dicking around to the next level, I recommend checking out asciimatics.
2
2
u/kati256 Sep 29 '18
Neato! I love how coding is 20% actual useful stuff, 75% neat tricks, and 5% UB.
3
u/Pr0ducer Sep 28 '18
I can't tell who's more bored at work, OP or the numerous others who're re-writing this to be more Pythonic. I mean, if we're going to waste time, waste it pythonically.
2
2
1
1
1
u/Kurt_W_Andrews Sep 28 '18
This is the kind of code I wrote in college, on a deck of punch cards, to create scratch paper on the system line printer.
1
1
1
1
1
1
1
1
u/_srt_ Sep 29 '18
Not that I know python. Last line for want more could also be this:
print('Want more?\n 1. Yes\n 2. No')
more = (int(input()) == 1)1
1
u/sentiao Sep 29 '18
I used to write seemingly useless software too, I actually still do; it's like a kata.
Here's my golfed version of your program:
k7=[' '+('_'*16)+' ','| __ __ |','| / \ / \ |','| __/ __/ |','| ____________ |','|_/_O________O__|']
while True:
size = int(input('How many k7s? Number of k7s per line:\n'))
print('\n'.join(['\n'.join([line*size for line in k7]+['']) * size]))1
1
u/Farranor Sep 30 '18 edited Sep 30 '18
Instead of if square == False: you can just do if not square:.
1
u/SilkTouchm Sep 30 '18 edited Sep 30 '18
What's the point of the "more" variable? it does nothing. You could replace the last three lines with:
more=int('0' if input() == '2' else '1')1
1
0
u/sean_oquin Feb 06 '19
Looking for Python developers to take a direct hire role in Rochester, NY. Let me know if you’re interested.
1
-1
u/Wubbywub Sep 28 '18
oh come on let him have some fun, why is everyone rushing to rewrite his code or feel the need to shove their "better" code
6
u/flobbley Sep 28 '18
I'm enjoying it! I don't know a huge amount about python and I'm finding the comments pretty helpful.
-3
0
Sep 28 '18
[deleted]
297
u/SwordOfKas Sep 28 '18
Employer: "What skills can you bring to the company?"
OP: "How many cassettes do you want?"
*types in '# python3 cassettes.py' and hits enter like it owes OP money*
Employer: "You're hired!"
54
u/superhappyrobots Sep 28 '18
Now make one that gives us more RAM!
51
u/phil_s_stein Sep 28 '18
--- cassette.py 2018-09-28 12:48:25.718464897 -0400 +++ ram.py 2018-09-28 12:48:24.766475414 -0400 @@ -1,2 +1 @@ -cassette = [' ______________ ', ' | __ __ |', r' | / \ / \ |', r' | __/ __/ |', ' | __________ |', r' |_/_o______o__|'] +cassette = [" _ __ __ _ _ __ ___", "| '__/ _` | '_ ` _ \\", "| | | (_| | | | | | |", "|_| __,_|_| |_| |_|"]11
78
u/glen_v Sep 28 '18
Employer: I want 23 cassettes.
OP: But that's not square.
Employer: Figure it out!!
15
u/aufstand Sep 29 '18
Easy! Draw 25 and erase the last two.
This consultation was free, any more tips will be very expensive.
4
7
1
0
0
Sep 28 '18
[deleted]
3
0
0
Sep 28 '18
[deleted]
2
u/Farranor Sep 30 '18
0
Sep 30 '18
[deleted]
1
u/Farranor Sep 30 '18
It was a fun hour or so, but doesn't seem to be getting a positive reception. Ah well.
0
Sep 30 '18
[deleted]
1
1
u/Farranor Oct 01 '18
Maybe I titled it wrong? Should I have mentioned cassettes? I'm better at coding than posting. =\
132
u/[deleted] Sep 28 '18
why not just
if size in sizesinstead of the for loop checking for each possibility and setting a flag?