In: Computer Science
Needs to be coded in Python.
Hello i am working on a project for my computer programming course and i am having trouble with one part.
The code needs to be able to produce two versions of an output given inputs by the user. for instance.
Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or descending.
Code with inputs (3, 9, True)
def build_percent_pattern(num1, num2, asc):
s = ''
l = (num2 - num1) + 1
if asc == 'true':
n = 0
s += '\n'
for i in range(l):
for i in range(num1, num2 + 1):
s += '\n' + ' ' * n
n += 1
fact = 0.1 * num1
for i in range(num1, num2 + 1):
s += '{:>.2f} '.format(fact * i)
num1 += 1
return s
else:
This code produces an output such as this.
0.90 1.20 1.50 1.80 2.10 2.40 2.70
1.60 2.00 2.40 2.80 3.20 3.60
2.50 3.00 3.50 4.00 4.50
3.60 4.20 4.80 5.40
4.90 5.60 6.30
6.40 7.20
8.10
I need to somehow manipulate this code to produce an output such as this.
8.10
6.40 7.20
4.90 5.60 6.30
3.60 4.20 4.80 5.40
2.50 3.00 3.50 4.00 4.50
1.60 2.00 2.40 2.80 3.20 3.60
0.90 1.20 1.50 1.80 2.10 2.40 2.70
The function build_percent_pattern(): needs to be able to produce both of these outputs based on whether the user chooses true or false.
If someone could just modify my code for the original output to just invert it like the second output that is all i really need. Thank you again!
Implemented the code as per the requirement. As python is
indentation specific, you may not get the formatted text while
copying the code,
so I'm attaching the screenshots of the code for reference. Please
make sure when you are executing the below code you have same
format, especially tabs.
Please comment if any modification required or if you need any help.
Called the method two times, one with true and another with false.
Code:
====
def build_percent_pattern(num1, num2, asc):
s = ''
l = (num2 - num1) + 1
if asc == 'true':
n = 0
s += '\n'
for i in range(l):
s += '\n'
n += 1
fact = 0.1 * num1
for i in range(num1, num2 + 1):
s += '{:>.2f} '.format(fact * i)
num1 += 1
else:
num1 = num2
n = 0
s += '\n'
for i in range(l,0,-1):
n += 1
fact = 0.1 * num1
for i in range(num1, num2 + 1):
s += '{:>.2f} '.format(fact * i)
num1 -= 1
s +='\n'
return s
print(build_percent_pattern(3, 9, "true"))
print()
print(build_percent_pattern(3, 9, "false"))
code screenshot:
=============
Output:
======