Python While Loop- Addition between two numbers [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Essentially, if the user inputs 2,12, the output should be 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10 + 11 + 12.
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
addition = num1 + num2
print (addition)
sum = 0
count = 1
while (count <= num1):
sum = sum + 1
count = count + 1
print ("Your total price comes to ", total_price)

Try the following code:
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
the_sum = 0
start = num1
end = num2 + 1
m = start
while m < end:
the_sum += m
m += 1
print ("Your total price comes to ", the_sum)

You have several problems... including that you are not actually doing anything. All you really do is do the addition line and printing what it is. Naturally, you should start with a for loop that will loop through the limits set by the user:
for i in range(num1, (num2)+1):
Now to add the numbers in between and keep track of the current sum, let's create two variables to keep track:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
Now add number to current_score and add one to number:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
Then finally print the result:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
print current_sum

Related

How to extract values from a while loop to print in python?

So I could print out the odd numbers. However, the output isn't what i want. It should look like 1+3+5+7 = 16 but I could not make it into a single line.
I couldn't figure out how to extract the values from the while loop as with my method it only gives the latest odd number which is 7 while 1,3 and 5 could not be taken out
num = int(input("Insert a postive integer:")) #4
oddNum = 1
total = 0
count = 1
while count <= num:
odd = (str(oddNum))
print (odd)
total = total + oddNum
oddNum = oddNum + 2
count += 1
print (odd + "=" + str(total))
#output will be:
'''
1
3
5
7
7=16
but it should look like 1+3+5+7=16
'''
An alternative method would be the use of:
range() method to generate the list of odd numbers
.join() method to stitch the odd numbers together (eg. 1+3+5+7)
f-strings to print odds together with the total = sum(odd_nums)
Code:
num = int(input("Insert a postive integer:")) #4
odd_nums = range(1, num * 2, 2)
sum_nums = "+".join(map(str, odd_nums))
print(f"{sum_nums}={sum(odd_nums)}")
Output:
1+3+5+7=16
Note:
Same but using two lines of code:
num = int(input("Insert a postive integer:")) #4
print(f"{'+'.join(map(str, range(1, num * 2, 2)))}={sum(range(1, num * 2, 2))}")
Output:
1+3+5+7=16
You are not storing old oddNum values in odd. With minimal changes can be fixed like this:
num = int(input("Insert a positive integer:"))
oddNum = 1
total = 0
count = 1
odd = ""
while count <= num:
total = total + oddNum
odd += f"{oddNum}"
oddNum = oddNum + 2
count += 1
odd = "+".join(odd)
print(odd + "=" + str(total))
There are a few options, you can either create a string during the loop and print that at the end, or create a list and transform that into a string at the end, or python3 has the ability to modify the default end of line with print(oddNum, end='').
Using a string:
num = int(input("Insert a postive integer:")) #4
oddNum = 1
total = 0
count = 1
sequence = ''
while count <= num:
sequence += ("+" if sequence != "" else "") + str(oddNum)
total = total + oddNum
oddNum = oddNum + 2
count += 1
print (sequence + "=" + str(total))
Using print:
num = int(input("Insert a postive integer:")) #4
oddNum = 1
total = 0
count = 1
while count <= num:
if count != 1:
print('+', end='')
print (oddNum, end='')
total = total + oddNum
oddNum = oddNum + 2
count += 1
print ("=" + str(total))
Alternatively using walrus (:=), range,print, sep, and end:
print(*(odd:=[*range(1,int(input('Insert a postive integer:'))*2,2)]),sep='+',end='=');print(sum(odd))
# Insert a postive integer:4
# 1+3+5+7=16

how do i write a python program to find if the input number is Armstrong number [duplicate]

This question already has answers here:
Armstrong numbers in python
(14 answers)
Closed 7 months ago.
def Counting(number):
count = 0
while(number > 0):
number = number // 10
count = count + 1
return count
number = int(input("Please Enter any number:"))
count = Counting(number)
i'm not sure what to do so i really can't do much
Program to test if a given number is an Armstrong number
def is_armstrong(n):
l = len(str(n))
t = 0
for d in str(n):
t += int(d) ** l
return t == n
print(is_armstrong(int(input('Enter a number: '))))
Output:
Enter a number: 153
True
Enter a number: 200
False
Hope this helps!
n = 153
s = n
b = len(str(n))
summ = 0
while n != 0:
r = n % 10
summ = summ+(r**b)
n = n//10
if s == summ:
print("The given number", s, "is armstrong number")
else:
print("The given number", s, "is not armstrong number")
Hope this helps. Thanks

Why does if loop is not working on this python program?

while adding two numbers, I want to check how many times carry is occurring –
for eg:
Input
Num 1: 451
Num 2: 349
Output
2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned.
def NumberOfCarries(num1, num2):
count = 0
l = str(num1)
i = 0
if i <= len(l):
n = num1 % 10
n2 = num2 % 10
sum = n + n2
print(f" num is {num1} and {num2} n1 is {n} and n2 is {n2} and sum is {sum}")
if sum > 9:
count += 1
num1 = num1 // 10
num2 = num2 // 10
i += 1
else:
num1 = num1 // 10
num2 = num2 // 10
i += 1
return count
num1 = int(input("> "))
num2 = int(input("> "))
print(NumberOfCarries(num1, num2))
Here loop is not working, only one time the sum is generating. I want to generate for each number in numb1. I tired with while, if and for. Please help me.I am new to this
I think you tried to do this:
def number_of_carries(num1, num2):
amplitude_num1 = len(str(num1))
amplitude_num2 = len(str(num2))
count = 0
i = 0
carry_over = 0
while i < amplitude_num1 or i < amplitude_num2:
n = num1 % 10
n2 = num2 % 10
sum_of_digits = n + n2 + carry_over
print(f" num is {num1} and {num2}, n1 is {n} and n2 is {n2}, carry digit from previous addition is {carry_over}, sum is {sum_of_digits}")
if sum_of_digits > 9:
count += 1
carry_over = 1
else:
carry_over = 0
num1 //= 10
num2 //= 10
i += 1
return count
num1 = int(input("> "))
num2 = int(input("> "))
print(number_of_carries(num1, num2))
But if you want to have a solution that would accept more that 2 numbers this could be modified to:
def number_of_carries(numbers):
amplitude = max(len(str(x)) for x in numbers)
count = 0
i = 0
carry_over = 0
for i in range(amplitude):
current_numbers = tuple(x % 10 for x in numbers)
sum_of_digits = sum(current_numbers) + carry_over
print(
f" numbers are {' '.join(str(x) for x in numbers)}, "
f"current_numbers are {' '.join(str(x) for x in current_numbers)}, "
f"carry digit from previous addition is {carry_over}, sum is {sum_of_digits}"
)
if sum_of_digits > 9:
count += 1
carry_over = sum_of_digits // 10
numbers = tuple(x // 10 for x in numbers)
return count
input_numbers = (198, 2583, 35)
print(number_of_carries(input_numbers))

While loop as a grade calculator in relationship to # of assignments completed

This is my while loop so far. I ask the user how many assignments they completed, which can only be between 1 and 10 (and graded out of 0 to 5).
However, if they answer anything below 10, I want the to code to automatically enter it as zero, instead of the user having to manually enter 0 for each assignment they have not completed.
Right now my code will ask the user to input a grade 10 times, but I only want to input grades based on the users answer of how many assignments they completed. So if they answered 7, the question would be presented 7 times, and three 0 marks would be automatically factored in to the average.
while True:
result = input("How many assignments did you complete? ")
if result.isdigit() and 0 <= int(result) <= 10:
break;
print ("Try a number between 1 and 10")
total = 0
gradeCount = 0
while gradeCount < 10:
grade = float(input('What was assignment score: '))
if grade < 0 or grade > 5:
print('It should be a number from 0 to 5')
else:
gradeCount += 1
total += grade
aaverage = total / 30 * 100
print('Average: ', aaverage)
while True:
result = input("How many assignments did you complete? ")
if result.isdigit() and 0 <= int(result) <= 10:
break;
print ("Try a number between 1 and 10")
total = 0
gradeCount = 0
while gradeCount < int(result):
grade = float(input('What was assignment score: '))
if grade < 0 or grade > 5:
print('It should be a number from 0 to 5')
else:
gradeCount += 1
total += grade
aaverage = total / 50 * 100
print('Average: ', aaverage)
Just count the number of assignments. Also you have to divide by 50, because of the 10 max assignments, so 50 points
while 1:
result = input("How many assignments did you complete? ")
if result.isdigit() and 0 <= int(result) <= 10:
result = int(result)
break
print ("Try a number between 1 and 10")
total = 0
for i in range(result):
while 1:
grade = float(input('What was assignment score: '))
if 0 <= grade <= 5:
break
else:
print('It should be a number from 0 to 5')
total += grade
aaverage = total / 10
print('Average: ', aaverage)

Why the following python is not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The following python coding is written to generate the triangle numbers between 1 to 55. But the coding is not working why ?
num = 1
sum = 0
while (num <= 10)
sum = sum + num
num = num + 1
print (sum, end=' ')
Missing colon :
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
or
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
Output
1 3 6 10 15 21 28 36 45 55
For 2.7
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum,
Your error is an error that is not followed by a while statement, followed by a () wrap and end of print that are not supported by default in Python 2.x.
The Corrected Code is:
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum

Categories

Resources