Display sum of every number from any specific range with User input - python-3.x

Using below code , i want to display sum for a particular 'n' range lies between two numbers.
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
i = 1
for i in range(start, end+1):
i+=1
print(i)
Yes, it concatenates two numbers by +1, now to display each number's sum one by one or if i want sum of all numbers from 1 to 100 (Say,1+2+3+4+5+6+7...n) or any range 'n' entered in particular, then how to go. Thanks

One of the Pythonic ways is
from itertools import accumulate
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
for index, value in enumerate(accumulate(range(start, end+1))):
print(f'Sum from {start} to {index} is : {value}')

Output is the same as from #SuyogShimpi, not really pythonic, more C-style, but maybe easier to understand.
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
current_sum = 0
for i in range(start, end+1):
current_sum += i
print(f'sum from {start} to {i} is {current_sum}')

you can use Numpy
Installing
pip install numpy
and use numpy.sum
import numpy as np
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
Sum = np.sum(np.arange(start, end+1))
print(Sum)

Related

Sumlist and appending problems

I am trying to create a program that takes in four numbers they can be negative or positive and it should sum all the numbers together and then print the sum.
My problem comes with the append line, I am trying to place it inside the list but it keeps coming up with an error and I am unsure why.
Here is the Code:
def sumList(NumList, list):
sum = 0
for num in list:
sum = sum + num
return sum
NumList = []
while (True):
number = int(input("please enter a number: "))
if (number != 0):
number.append(number, NumList) #Here keeps coming up as an error
else:
sumList()
break
print(NumList)
Thank you for having the time to read this.
Honestly, your code is a mess. This works:
numlist = []
number = int(input("please enter a number: "))
while number != 0:
numlist.append(number)
number = int(input("please enter a number: "))
print(sum(numlist))
To sum the values of an iterable, you can simply use the builtin sum function. And to append something to a list, use list.append(value)
It should be NumList.append(number) or NumList.extend(number)
code snippet:
def sumList(NumList, list):
sum = 0
for num in list:
sum = sum + num
return sum
NumList = []
while (True):
number = int(input("please enter a number: "))
if (number != 0):
NumList.append(number) #correct this
else:
sumList()
break
print(NumList)

I was supposed to count the unique digits in a number but getting this int object not iterable on 4th line and not sure how to fix it

x = int(input("Enter the number: "))
count = 0
for elements in range(0,10):
for i in (x):
if elements == i:
count += 1
break
print()
That error raise because an integer is not an iterate object unlike strings, list, etc. So what you can do it's just work with a string, then use set (which get the unique values) and then get the length of it and you won't need to traverse with a for loop as below:
x = input("Enter the number: ")
unique_digits = set(x)
print(len(unique_digits))
Hope it will help you :)
x = input("Enter number: ")
count = 0
for elements in range(10):
for t in x:
if (int(t)==elements):
count += 1
break
print(count)

Want to print consecutive 1's in

from array import *
n=int(input("Enter the number"))
m=bin(n)
#print(m)
a=array("u",[])
a=int(m[2:])
print(a)
count=0
for i in range(a):
if(a[i]=="1" and a[i+1]=="1"):
count=count+1
print(count)
Here i want to print'ONLY' the number of consecutive 1's that appear in a binary number.
If your logic is correct try this,
n = int(input('Enter a number : '))
nb =bin(n)
l = list(nb)[2:]
count = 0
for i in range(len(l)-1):
if l[i]=='1' and l[i+1]=='1':
count += 1
print(count)

How to fix this

This code is supposed to find all the primes in a given range. But something is very wrong here
import math
def display(num, truth):
if truth:
print(num)
lower = int(input("Enter lower limit :"))
upper = int(input("Enter upper limit :"))
for x in range(lower, upper, 1):
b = 2
c = True
a = math.sqrt(x)
while b < a:
if x%b != 0:
continue
else:
c = False
break
display(x,c)
print("Done")
I expect that it should output, say between 2 and 6:
Enter lower limit :2
Enter upper limit :6
2
3
5
Done
But the output is (for same range)
Enter lower limit :2
Enter upper limit :6
2
3
4
Note that 'Done' does not appear
And when I try to close the shell Python warns that the program is still running.
I am not sure to understand some of the code you have written.
How About something like this:
import math
lower = int(input("Enter lower limit :"))
upper = int(input("Enter upper limit :"))
for x in range(lower,upper + 1):
# prime numbers are greater than 1
if x > 1:
for i in range(2,x):
if (x % i) == 0:
break
else:
print(x)
print("Done")

Making a matrix in python 3 without numpy using inputs

I want to have two inputs : a,b or x,y whatever...
When the user inputs say,
3 5
Then the shell should print a matrix with 3 rows and 5 columns also it should fill the matrix with natural numbers( number sequence starting with 1 and not 0).
Example::
IN :2 2
OUT:[1,2]
[3,4]
If your objective is only to get the output in that format
n,m=map(int,input().split())
count=0
for _ in range(0,n):
list=[]
while len(list) > 0 : list.pop()
for i in range(count,count+m):
list.append(i)
count+=1
print(list)
I am going to give a try without using numpy library.
row= int(input("Enter number of rows"))
col= int(input("Enter number of columns"))
count= 1
final_matrix= []
for i in range(row):
sub_matrix= []
for j in range(col):
sub_matrix.append(count)
count += 1
final_matrix.append(sub_matrix)
Numpy library provides reshape() function that does exactly what you're looking for.
from numpy import * #import numpy, you can install it with pip
n = int(input("Enter number of rows: ")) #Ask for your input, edit this as desired.
m = int(input("Enter number of columns: "))
x = range(1, n*m+1) #You want the range to start from 1, so you pass that as first argument.
x = reshape(x,(n,m)) #call reshape function from numpy
print(x) #finally show it on screen
EDIT
If you don't want to use numpy as you pointed out in the comments, here's another way to solve the problem without any libraries.
n = int(input("Enter number of rows: ")) #Ask for your input, edit this as desired.
m = int(input("Enter number of columns: "))
x = 1 #You want the range to start from 1
list_of_lists = [] #create a lists to store your columns
for row in range(n):
inner_list = [] #create the column
for col in range(m):
inner_list.append(x) #add the x element and increase its value
x=x+1
list_of_lists.append(inner_list) #add it
for internalList in list_of_lists: #this is just formatting.
print(str(internalList)+"\n")

Resources