For loop multiply in some range - python-3.x

I have a task for my college in which I must program some very simple logic for multiplying numbers in range like 1 to 5 and than multiply like 1*2*3*4*5. And this way for any input number. For 7 it would be 1*2*3*4*5*6*7.
This is my modest code which is not finished because of no idea how to do it .Help, please..
number = int(input("Enter a number:"))
number += 1
for i in range(1,number):
a = i*(number*
print(a)

Try this:
In [1774]: number = int(input("Enter a number:"))
In [1775]: a = 1
In [1776]: for i in range(1, number+1):
...: a *= i
In [1781]: a
Out[1781]: 120
a's value is 120 which is basically (1*2*3*4*5). Hope this helps.

Related

Given a number N, print sum of all even numbers from 1 to N. Using While loop in python

number = int(input())
count = 2
sum = 0
while count <= number:
if count%2 == 0:
sum = sum + count
count = count + 1
print(sum)
I did this but still, the code is wrong.
Given the code above, your main issue is indentation:
number = int(input())
count = 2
sum = 0
...
count and sum should start where number starts as well:
number = int(input())
count = 2
sum = 0
...
Also, you could make your code more efficient by incrementing your count by 2, therefore getting rid of the if statement:
number = int(input())
count = 2
Sum = 0
while count <= number:
Sum += count
count += 2
print(Sum)
And of course, as Alex P said, avoid using sum as a variable name as it is bad practice. Use Sum, or any other variation that doesn't look like the method sum().

why using variable and a number in loop show different outcomes

a = 1
for number in range(5):
a = a + number * 5
print(a)
#
for number in range(5):
a = 1 + number * 5
print(a)
In the first loop, you retain the value of a, and add to it number * 5 in each iteration.
In the second loop, you always take 1 and add to it number * 5.
You can, of course, achieve the same behavior with variables, you just need to use another one:
first = 1
for number in range(5):
a = first + number * 5
print(a)

I want to improve speed of my algorithm with multiple rows input. Python. Find average of consequitive elements in list

I need to find average of consecutive elements from list.
At first I am given lenght of list,
then list with numbers,
then am given how many test i need to perform(several rows with inputs),
then I am given several inputs to perform tests(and need to print as many rows with results)
every row for test consist of start and end element in list.
My algorithm:
nu = int(input()) # At first I am given lenght of list
numbers = input().split() # then list with numbers
num = input() # number of rows with inputs
k =[float(i) for i in numbers] # given that numbers in list are of float type
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
print(round(sum(k[int(a):(int(b)+1)])/(-int(a)+int(b)+1),6)) # round up to 6 decimals
But it's not fast enough.I was told it;s better to get rid of "while" but I don't know how. Appreciate any help.
Example:
Input:
8 - len(list)
79.02 36.68 79.83 76.00 95.48 48.84 49.95 91.91 - list
10 - number of test
0 0 - a1,b1
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 7
2 7
Output:
79.020000
57.850000
65.176667
67.882500
73.402000
69.308333
66.542857
69.713750
68.384286
73.668333
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
Replace your while-loop with a for loop. Also you could get rid of multiple int calls in the print statement:
for _ in range(int(num)):
a, b = [int(j) for j in input().split()]
You didn't spell out the constraints, but I am guessing that the ranges to be averaged could be quite large. Computing sum(k[int(a):(int(b)+1)]) may take a while.
However, if you precompute partial sums of the input list, each query can be answered in a constant time (sum of numbers in the range is a difference of corresponding partial sums).

How to calculate the annual rate in python 3

Here's the code:
P = int(input("Enter starting principle please.\n"))
n = int(input("Enter Compound interest rate.(daily, monthly, quarterly,half-year, yearly)\n"))
r = float(input("Enter annual interest amount. (decimal)\n"))
t = int(input("Enter the amount of years.\n"))
t = 1
while t-1 <= 5-1 :
final = P * (((1 + (r/n)) ** (n*t)))
t += 1
print ("The final amount after", round(t-1), "years is", round(final,2))
When I tried to input:
1000
1
0.02
2
it will result like this:
Enter starting principle please.
Enter Compound interest rate.(daily, monthly, quarterly, half-year, yearly)
Enter annual interest amount. (decimal)
Enter the amount of years.
The final amount after 1 years is 1020.0
The final amount after 2 years is 1040.4
The final amount after 3 years is 1061.21
The final amount after 4 years is 1082.43
The final amount after 5 years is 1104.08
The problem is, it will not return to the require input number of years (e.g when I tried to input 2 years it will print up to 5 years)
Why are you setting t from input, but then immediately ignoring the input and overwriting its value with t = 1?
And where is this 5 coming from: while t-1 <= 5-1
Essentially you need a new variable for what you're doing in the loop, separate from t. And "magic numbers" like 5 appearing in code for no reason are something to be avoided.
P = int(input("Enter starting principle please.\n"))
n = int(input("Enter Compound interest rate.(daily, monthly, quarterly, half-year, yearly)\n"))
r = float(input("Enter annual interest amount. (decimal)\n"))
t = int(input("Enter the amount of years.\n"))
for t in range(1, t+1):
final = P * (((1 + (r/n)) ** (n*t)))
t += 1
print ("The final amount after", round(t-1), "years is", round(final,2))

Python - Negative numbers not adding, but positive numbers do

I am supposed to add up the rows and the grand total of all the numbers. I can add the grand total well, but I am unable to add the row that has negative numbers only. The following code adds up the positive numbers but do not add up the negative numbers correctly.
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + x)
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
When the user input is:
1,1,-2 -1,-2,-3 1,1,1
My output is: 0
1
3
-3
instead of: 0
-6
3
-3
I know it has something to do with the first for loop, but I can't figure it out. When I try this:
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + (numbers[x]))
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
I get the error message:
File "list.py", line 14, in
sumRow = (sumRow + (numbers[x]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Why doesn't my code add up the negative numbers? Any help is greatly appreciated!
Where you say
sumRow = (sumRow + (numbers[x]))
To add integers you say 1+1, not (1+(1)) this would be adding to lists so you could change that.
From my understanding numbers is an array as well, so saying
numbers[x]
Will give you many numbers. What you want is the total for every row, and the total of all rows. Heres a program that does this. I am assuming that your program automatically gets numbers from the user input.
grandTotal = 0
for row in numbers:
#for each row we find total amount
rowTotl = 0
for value in row:
#for each column/ value we add tot the row total
rowTotl += value
print(rowTotl)
#add this row's value to the grandTotal before we move on
grandTotal += rowTotl
#After all adding we print grand total
print(grandTotal)
The reason your program doesn't add negative numbers, is really because the row totals are not adding numbers at all. They are just adding the indexes, rather than the value, so they don't work for positive number either. The grand total works because you are adding all the values properly, rather than adding the indexes. FYI,
for index in range(len(numbers)) :
does not give you the values, but rather : 0,1,2,3,4,5,6...(indexes) till the end of the range, to get the value numbers you would do
for value in numbers:

Resources