patternpythonModerate
Python odd number diamond
Viewed 0 times
odddiamondnumberpython
Problem
I worked up a quick diamond program that takes only odd numbers while returning an error with even numbers or 0.
The code looks rather ugly to me, especially with the recurring
The code looks rather ugly to me, especially with the recurring
int(). I haven't found out how to clean that up with my ENTER to quit.while True:
number = raw_input("Enter an odd number or hit ENTER to quit program: ")
if number == "":
print "\nQuitting program...\n"
break
elif int(number) == 0:
print "\nCannot print symbols for a zero value, try again.\n"
elif int(number) % 2 == 0:
print "\nThat's an even number, try again.\n"
else:
for i in range((int(number))+1):
if i % 2 == 0:
None
else:
print ("*" * i).center(int(number))
for i in range((int(number))-1):
if i % 2 == 0:
None
else:
print ("*" * ((int(number)) - (i + 1))).center(int(number))Solution
- You can test for emptiness of
numberby justif not number
- After testing for emptiness, you could go straight to an int by adding
number = int(number)
- Inside the loops, when you test for
i % 2 == 0:, and then putNoneas the loop body (that should really bepass), you could just test forif i % 2 != 0:, and replaceNonewith theelsebody, and remove theelse.
0is even, so you can remove that test.
- You can replace
int(number) % 2 == 0withnot int(number) % 2.
- Looking at the
forloops, therangecould be changed torange(1, number-1, 2), removing the need for anifstatement.
- You can also invert the last
range, so the subtracting fromnumberand1is not needed.
- Python automatically adds newlines, so the
\ns aren't really needed.
That leaves us with (after removing a few useless brackets):
while True:
number = raw_input("Enter an odd number or hit ENTER to quit program: ")
if not number:
print "Quitting program..."
break
number = int(number)
if not number % 2:
print "That's an even number, try again."
else:
for i in range(1, number + 1, 2):
print ("*" * i).center(number)
for i in range(number - 2, 0, -2):
print ("*" * i).center(number)If you want to go functional, you can move your user input checking into a function. In this function you could also check if the user inputs a non-number, such as
abc, by catching the exception or using str.isdigit:def get_input():
while True:
number = raw_input("Enter an odd number or hit ENTER to quit program: ")
if not number:
return # return None when the user wants to quit
if number.isdigit() and int(number) % 2:
return int(number) # return the number, as its odd
print "That's not a valid input (must be an odd integer), try again."With this function, the rest of your program reduces to:
for number in iter(get_input, None):
for i in range(1, number + 1, 2):
print ("*" * i).center(number)
for i in range(number - 2, 0, -2):
print ("*" * i).center(number)
print "Quitting program..."iter calls the function and gives its value until the function returns None (the sentinel value).On a stylistic note:
- You should always have spaced around operators, so
range(int(number)+1)should berange(int(number) + 1)(that's PEP8).
- You may want to add a big fat comment at the start of your program describing what it does.
Also, nice use of
str.center, it avoided having too many loops!Code Snippets
while True:
number = raw_input("Enter an odd number or hit ENTER to quit program: ")
if not number:
print "Quitting program..."
break
number = int(number)
if not number % 2:
print "That's an even number, try again."
else:
for i in range(1, number + 1, 2):
print ("*" * i).center(number)
for i in range(number - 2, 0, -2):
print ("*" * i).center(number)def get_input():
while True:
number = raw_input("Enter an odd number or hit ENTER to quit program: ")
if not number:
return # return None when the user wants to quit
if number.isdigit() and int(number) % 2:
return int(number) # return the number, as its odd
print "That's not a valid input (must be an odd integer), try again."for number in iter(get_input, None):
for i in range(1, number + 1, 2):
print ("*" * i).center(number)
for i in range(number - 2, 0, -2):
print ("*" * i).center(number)
print "Quitting program..."Context
StackExchange Code Review Q#68365, answer score: 11
Revisions (0)
No revisions yet.