This is as far as i have gotten and dont know where go on from here.
result = 0
x = int(input('First: '))
y = int(input('Last: '))
y = y+1
for i in range((print(x+1, y)):
print(result)
This should work:
num1 = int(input("Enter first integer: "))
num2 = int(input("Enter second integer: "))
sumNum1Num2N = 0
print("Let's sum all the integers in this range [%d, %d]" %(num1, num2))
#This shows up the sum being done
for i in range(num1, num2 + 1):
sumNum1Num2N = sumNum1Num2N + i
print(sumNum1Num2N, " " , end="")
print()
print("The sum of the numbers between %d and %d is %d" %(num1, num2, sumNum1Num2N))
If you want to show the integers between 2 numbers.
for i in range(num1, num2 + 1):
print(i)
But if you want a fastest way to sum 2 numbers use the built-in functions sum and range:
m_sum = sum(range(num1, num2 + 1)) #[num1, num2]
Mathematically you can achieve also the sum of the numbers between A and B, where B > A:
B*(B + 1)/2 - (A - 1)*A/2
= (B^2 + B - A^2 + A) / 2
= ((B - A)*(B + A) + (B + A)) / 2
= (B + A) * (B - A + 1) / 2
But I think this is more complicated than the other 2 methods, even though the mathematical procedure is trivial ;)
Does this do what you need?
x = int(input('First: '))
y = int(input('Last: '))
for i in range(x,y+1):
print(i)
Try:
>>> x = int(input('First: '))
First: 1
>>> y = int(input('Last: '))
Last: 10
>>> print(' + '.join(str(e) for e in range(x+1,y)),'=',sum(range(x+1,y)))
2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 44
Related
I'm new to python and want to make a program that generates Pi with the given decimal numbers. Problem is that I don't know how to check if the user has inputted a positive number.
This is the function that generates Pi (I don't know for sure how it works)
def PiBerekening(limiet):
q = 1
r = 0
t = 1
k = 1
n = 3
l = 3
decimaal = limiet
teller = 0
while teller != decimaal + 1:
if 4 * q + r - t < n * t:
# yield digit
yield n
# insert period after first digit
if teller == 0:
yield '.'
# end
if decimaal == teller:
print('')
break
teller += 1
nr = 10 * (r - n * t)
n = ((10 * (3 * q + r)) // t) - 10 * n
q *= 10
r = nr
else:
nr = (2 * q + r) * l
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
And this is how I ask the user how many decimals he wants to see
while not verlaatloop:
try:
pi_cijfers = PiBerekening(int(input("With how many decimals would you like to calculate Pi?")))
assert pi_cijfer > 0 # This is one of the things I've tried but I get the "NameError: name 'pi_cijfer' is not defined" error and I don't know what to do to check if the inputted number is negative
except ValueError:
print("This isn't a valid number, try again")
except AssertionError:
print("This number is negative, try again")
else:
verlaatloop = True
This is how I show the calculated Pi
for pi_cijfer in pi_cijfers:
print(pi_cijfer, end='')
You can first validate the input and then pass it to the PiBerekening function. Something like this:
while not verlaatloop:
try:
no_decimals = int(input("With how many decimals would you like to calculate Pi?"))
if no_decimals > 0:
pi_cijfers = PiBerekening(no_decimals)
#assert pi_cijfer > 0 # This is one of the things I've tried but I get the "NameError: name 'pi_cijfer' is not defined" error and I don't know what to do to check if the inputted number is negative
except ValueError:
print("This isn't a valid number, try again")
except AssertionError:
print("This number is negative, try again")
else:
verlaatloop = True
I want to input 12 numbers and have it by column and rows but Im having a problem how to do that
lst = []
num = int(input("Enter 12 numbers:"))
for n in range(num):
numbers = int(input(' '))
lst.append(numbers)
I want the output to look ike this:
Thr numbers will only depend on the entered numbers
1 2 3 4
5 6 7 8
9 1 2 3
Just use a loop to print the list on the amount of rows you want. Say you want to print lst on n rows:
row_len = len(lst) // n
for i in range(0, len(lst), row_len):
print(lst[i: i + row_len])
lst = []
num = int(input("Enter 12 numbers:"))
while num > 0:
lst.append(num % 10)
num = int(num / 10)
lst.reverse()
for i in range(3):
temp = ""
for j in range(4):
index = i * 3 + j
temp += str(lst[index]) + " "
print(temp)
Task:
Write a program that reads from the standard input and will return the:
sum
difference
product
of all elements in the given list.
Input
An integer n (1 <= n <= 500) that denotes the number of elements in the list. The following n integers are the next elements of the list.
Output
Three integers:
sum
difference
product
of all the elements of the list.
And these are my ideas, but I still get a mistake
from sys import stdin
def Simple_list_arithmetic():
print("Enter a positive number: ")
n = int(stdin.readline())
l = []
if n >= 1 and n <= 500:
for i in range(1, n+1):
l.append(i)
#print(l)
suma = 0
for add in range(0, len(l)):
suma = suma + l[add]
print(suma)
#return suma
difference = 2
for substract in range(0, len(l)):
difference = difference - l[substract]
print(difference)
#return difference
product = 1
for increase in range(0, len(l)):
product = product * l[increase]
print(product)
return suma, difference, product
else:
print("Wrong number.")
still wrong
suma = 0
q = [suma + l[add] for add in range(0, len(l))]
print(sum(q))
difference = 2
w = [difference - l[substract] for substract in range(0, len(l))]
print(list(w))
product = 1
e = [product * l[increase] for increase in range(0, len(l))]
print(sum(e))
still wrong
if n >= 1 and n <= 500:
for x in range(1, n+1):
print(x)
print("\n")
suma = 0
for add in range(1, n+1):
suma = suma + add
print(suma)
difference = 2
for substract in range(1, n+1):
difference = difference - substract
print(difference)
product = 1
for increase in range(1, n+1):
product = product * increase
print(product)
with map() and lambda still wrong
t = list(map(lambda add: suma + l[add], range(0, len(l))))
#return sum(t)
y = list(map(lambda increase: increase * l[increase], range(0, len(l))))
#print(y[-1])
#print(y)
try this:
x = int(input("Enter a positive number: "))
if x >= 1 and x <= 500:
sum = 0
for i in range(x,501):
sum +=i
print(sum)
diff = 2
for i in range(x,501):
diff -= i
print(diff)
prod = 1
for i in range(x, 501):
prod *= i
print(prod)
Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of Y odd numbers from X including it if is the case. For example: for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13 for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13
Try this I was just working on it.
x = int(input("Enter Value of X: "))
y = int(input("Enter Value of Y: "))
result = 0
itt = 0
while itt != y:
if x % 2 == 0:
x += 1
else:
itt += 1
result += x
x += 1
print(result)
import time
total = 0
pos = 0
zeroes = 0
neg = 0
print('This program will add any seven numbers for you')
time.sleep(2)
print()
a = int(input('Please enter the first number: '))
total = total + a
if a > 0:
pos = pos + 1
elif a == 0:
zeroes = zeroes + 1
elif a < 0:
neg = neg + 1
time.sleep(2)
b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
pos = pos + 1
elif a == 0:
zeroes = zeroes + 1
elif a < 0:
neg = neg + 1
time.sleep(2)
c = int(input('Please enter the third number: '))
total = total + c
if c > 0:
pos = pos + 1
elif c == 0:
zeroes = zeroes + 1
elif c < 0:
neg = neg + 1
time.sleep(2)
d = int(input('Please enter the fourth number: '))
total = total + d
if d > 0:
pos = pos + 1
elif d == 0:
zeroes = zeroes + 1
elif d < 0:
neg = neg + 1
time.sleep(2)
e = int(input('Please enter the fifth number: '))
total =total + e
if e > 0:
pos = pos + 1
elif e == 0:
zeroes = zeroes + 1
elif e < 0:
neg = neg + 1
time.sleep(2)
f = int(input('Please enter the sixth number: '))
total = total + f
if f > 0:
pos = pos + 1
elif f == 0:
zeroes = zeroes + 1
elif f < 0:
neg = neg + 1
time.sleep(2)
g = int(input('Please enter the seventh number: '))
total = total + g
if g > 0:
pos = pos + 1
elif g == 0:
zeroes = zeroes + 1
elif g < 0:
neg = neg + 1
time.sleep(2)
print()
print('The sum of your entries is: ', + total)
time.sleep(2)
print()
print('You entered', + pos, 'positive numbers')
time.sleep(2)
print()
print('You entered', + zeroes, 'zeroes')
time.sleep(2)
print()
print('You entered', + neg, 'negative numbers')
print()
time.sleep(3)
Hello! I have the variable 'neg' keeping a running total of all of the negative numbers that the user enters. It seems as if the negative numbers aren't always being added to the 'neg' running total at the end of the code.
I've been working with Python 3x for about a week now, so be gentle :)
Thanks in advance for the help!
Edit: I have reworked this into a (working) loop per the advice of Kevin, is this a good loop? It seems to work, I'm just looking for pointers as I'm kind of struggling with Python logic. Big thanks to Kevin, wish I could upvote you!
New code posted below:
import time
sums = 0
pos = 0
neg = 0
zero = 0
numb = 0
user_numb = 0
running = True
print('This program will add any 7 numbers for you')
time.sleep(.5)
print()
while running:
user_numb = int(input('Please enter a number: '))
sums = user_numb + sums
numb = numb + 1
print()
if user_numb > 0:
pos = pos + 1
elif user_numb < 0:
neg = neg + 1
elif user_numb == 0:
zero = zero + 1
if numb == 7:
running = False
print()
time.sleep(2)
print('The sum of the numbers entered was: ', + sums)
print()
time.sleep(2)
print('You entered', + pos, 'positive numbers')
print()
time.sleep(2)
print('You entered', + neg, 'negative numbers')
print()
time.sleep(2)
print('You entered', + zero, 'zeroes')
print()
print()
time.sleep(3)
In the section with b you forgot in the copy/pasted code to change all 'a' into 'b' and have still twice an 'a' there:
b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
pos = pos + 1
elif a == 0: # CHANGE TO 'b'
zeroes = zeroes + 1
elif a < 0: # CHANGE TO 'b'
neg = neg + 1
so you see wrong results only in case the second number is a 0 or negative.