Question
Plz use nested loops , read the question carefully, i need help step by step solution using ( spyder)..
Exercise 2 Write a Python program (Lab7-ex2.py) that reads from the user an integer n, displays the triangular number sequence up to the n term (ie. 1,3, 6, 10,. nterm). The triangular of a number n is calculated as n(n+1)/2 where n is a nonzero positive number. Note that your program should display a suitable error message if n is entered as a zero or a negative number. n Triangular 4 10 5 15 Figure 2. Exercise 2 Sample Run for n-6
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We need not use NESTED LOOPS for this problem. Only one Loop is sufficient. This is a simple problem where we use concepts like:

  1. If-else conditions
  2. for loop
  3. print() function
  4. how to create a function and call a function
  5. How to use INTEGER DIVISION ( // )

I have made things clear in the source code and explained every line of code.

Please refer to the comments I provided in the source code. I have added the image of source code along with the test cases. ( Input and output ).

I have used SPYDER as you asked.

NOTE:

We take a//2 integer division to get the output in integer format. ex: 10//2==> 5 and 9//2=4

If we use a/2 we get a decimal format. ex: 10/2 ==> 5.0 and 9/2==>4.5Spyder (Python 3.6) File Edit Se Source Run Debug Consoles Projects Tools View Help Editor-C:UsersHP,spyder-py3temp.py temp.py N source Console Object 1# This function gives the NthTriangular Number 2#Example : Ned 3 #Triangular num.4(4+1)//2 4# 5# 6# Usage (4 s)77 ” 20//2 -16 ariable explorer Fle explorer Heb 7 def get triangular num(n): Python console return (n (nt1))/2 | Console 1/A Enter the N value: 6 1#Read the input here 2 n-int(input( Enter the N value: ) Triangular 4#If the number is negative or zero, then it is error 5 if n<=0: 6 print( Invalid Number.... Number should be greater than zero..) 7#The number is valid 8 else: TEST CASE-1 10 15 9 print Triangular) 1 print(--) In [2]: runfile(C:/Users/HP/.spyder-py3/temp.py, wdir-C:/Users/HP/. spyder-py3 #Loop from 1 to N and call the function(i) to get the Triangular Number for iin ange(1,n): 3 Enter the N value: 1e print(i, get triangular_num(i)) Triangular TEST CASE-2 10 15 21 28 36 45 SOURCE CODE In [3]: | IPython console History log Permissions: RW End-of-lines: CRLF Encoding: UTF-8 Line: 18 Column: 6 Memory: 82% 18:42 O Type here to search 29-10-2018 2=============================================================================

NOTE: Please hit the LIKE button if you like the Solution and appreciate our time. It would really boost us to help you better.
If there are any doubts, HIT the LIKE and comment down here.. we are right here to help you.!!

Happy Learning..!!

Add a comment
Know the answer?
Add Answer to:
Plz use nested loops , read the question carefully, i need help step by step solution...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • plz solve it using ( python) spyder. use lists, loops,,,etc not allowed( def, ...) Exercise 1...

    plz solve it using ( python) spyder. use lists, loops,,,etc not allowed( def, ...) Exercise 1 Write a Python program that reads from user the names and temperatures of n cities (n must be> 0) into two lists: a list to hold the names of the cities and another one to store the temperatures of each city. Then using these two lists, your program should find and display the following information as shown in sample output below . The average...

  • I need help using python ( spyder) only use files , loops if needed is statement do not use , def...

    i need help using python ( spyder) only use files , loops if needed is statement do not use , def function or any other. Exercise 1: Daily temperature is recorded for some weeks in files (templ.txt", temp2.txt, and temp3.txt; provided in the MOODLE). The first line contains number of weeks and the rest of the lines each represent the week number followed by temperature on the seven days of that week (see samples input files below). Write a python...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • Use only if else nested if else only otherwise your answer won't be entertained. Problem 1(b):...

    Use only if else nested if else only otherwise your answer won't be entertained. Problem 1(b): Write a program that gives remainder without using modulus operator and loops. The first number entered will be dividend and second number entered will be divisor. Sample input: 15 6 Sample output: Remainder is 3 In a right triangle, the square of the length of one side is equal to the sum of the squares of the length of the other two sides. Write...

  • Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

    Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. We use the above series for approximating (π^2)/8...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Hi I need some help in C programing class and I doing one of the exercise...

    Hi I need some help in C programing class and I doing one of the exercise so that I can get better at coding. Suppose you are asked to design a software that helps an elementary school student learn multiplication and division of one-digit integer numbers. The software allows the student to select the arithmetic operation she or he wishes to study. The student chooses from a menu one of two arithmetic operations: 1) Multiplication and 2) Division (quotient). Based...

  • This is a repost of my question from before. I am having trouble writting the code...

    This is a repost of my question from before. I am having trouble writting the code for this project. Below is the project description with various methods and classes the code in C++ should implement. Project Description The goal of this project is to design and develop C++ code and algorithms to control a bank of elevators, which services many floors and transports people “efficiently ", that is, as per the given specifications. A second goal is to effectively employ...

  • I'm using Python 3.7 with Spyder I need the full code and the same output as...

    I'm using Python 3.7 with Spyder I need the full code and the same output as the sample above Resources file: https://drive.google.com/file/d/1e5a21ZKRj2H_jOnWvg7HcjUKjJlY84KE/view -   https://drive.google.com/file/d/1XIA41ra8AaKjFuxO5VpwVkn90bxwDyB5/view Task description Baye's Theorem can be used to build many machine learning applications, including spam classifier Spam Classifier in Python from scratch is a tutorial which explains how to use Bave's Theorem and Python to develop a spam classifier step by step To train the spam classifier, one dataset "spam.csv" is used in the program Its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT