Question

In: Computer Science

Needs to be coded in Python. Hello i am working on a project for my computer...

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!

Solutions

Expert Solution

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:

======


Related Solutions

Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
Hello all, I am toward the beginning of my semseter and am still pretty rusty working...
Hello all, I am toward the beginning of my semseter and am still pretty rusty working with code. here is my following goal: basically create a structure and populate an array from a txt file which contains 98 45.70 72 15.0 12 0.0 56 43.26 83 123.0 28 931.96 123 12.38 "Create a struct named familyFinance that contains 2 members: ▪ int acctNos ▪ float balance o (prior to the while loop) Create an array financeAry (size 10), of type...
I am working on my Business Statistics problem and it needs to be at least 100...
I am working on my Business Statistics problem and it needs to be at least 100 words. Here is what It asks: Nothing is for certain. What are acceptable confidence levels for things like you car starting or your paycheck showing up on time? How about a pharmacist giving you the correct medicine?
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am having trouble getting started on my project and building these functions. How do...
Hello, I am having trouble getting started on my project and building these functions. How do I build a function that continuously adds new "slices" to the list if they are below/above the size limit? I didn't copy the entire problem, but just for reference, when the code is run it will take user input for size limit (L), time cost for a random slice(R), and time cost for an accurate slice(A). Question: In real life, a steak is a...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
I am working on a project for my business class to creat a non profit organization!...
I am working on a project for my business class to creat a non profit organization! I came up with offering free WIFI/internet to everyone in the comfort of their own homes (without having to run to starbucks everytime you need to look something up) I am having trouble answering these two questions regarding my non profit so any advice would help! 5. Describe the type of legal structure that would be most appropriate for the non-profit organization. 6. Discuss...
I am working on a project where I have to create my own organization (stem cell...
I am working on a project where I have to create my own organization (stem cell research) I need to go further in depth about the program or organization in terms of describing the program implementation and the project evaluation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT