Python times table using while - python

Why in python a times table program doesn't work like this?
n = int(input("Type a number: "))
count = 10
while count < 0:
print(f"{count} x {n} = {n * count}")
count = count - 1
But this works correctly:
n = int(input("Type a number: "))
for i in range(1, 11):
print(f"{i} x {n} = {n * i}")
The result is (or should be), for example:
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
As a beginner I need to understand... and is it possible to use while in that situation?
This is python 3.x

You want to start count from 1 so that your loop increments rather than decrements. Your while loop conditional check is also incorrect as count is never less than 0. Try this:
n = int(input("Type a number: "))
count = 1
while count <= 10:
print(f"{count} x {n} = {n * count}")
count = count + 1

You need to set the condition in the while to be true to go through the loop. Thus we will say do while count is greater than or equal to zero.
n = int(input("Type a number: "))
count = 10
while count >= 0:
print(f"{count} x {n} = {n * count}")
count = count - 1

Related

return statement is not executing loops but print does

here i am getting multiplication table using print statement.
userNum = int(input("enter a number:"))
def multiply(n):
comp = 10
count = 0
while comp > count:
count+=1
z = n*count
y = "{1} * {2} = {0}".format(z,n,count)
print(y)
multiply(userNum)
here i am getting only 5 *1 = 5.it does not execute other despite using loops
userNum = int(input("enter a number:"))
def multiply(n):
comp = 10
count = 0
while comp > count:
count+=1
z = n*count
y = "{1} * {2} = {0}".format(z,n,count)
return y
a = multiply(userNum)
print(a)
This is because in Python, the return statement automatically exits the function, because a function can only return one item. Hence, in the second scenario, once y was returned, it exited the function, so it only printed the first item in the while loop.
In response to your comment
"so how to return loops in function without print statement?any
solution?"
You could write a nested loop in order to loop over a list, which stores the items returned by the function. For example:
userNum = int(input("enter a number:"))
y_values = []
def multiply(n):
comp = 10
count = 0
while comp > count:
count+=1
z = n*count
y = "{1} * {2} = {0}".format(z,n,count)
y_values.append(y)
multiply(userNum)
You can then loop over the list to print the items:
for y in y_values:
print(y)
OUTPUT, as expected:
enter a number:5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

A for loop to print dictionary items once without repeating the print function

I am new to Python and Stackoverflow in general, so sorry if my formatting sucks and i'm not good at enlish.But i have a problem with this code.
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print('They are',n,end=' ')
The result of the code when ran comes out looking like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 They are 2 They are 3 They are 5 They are 7 They are 11 They are 13 They are 17 They are 19 They are 23 They are 29 They are 31 They are 37
but i want it like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37
If you're completely determined not to use the print function more than once inside the loop, you could set a flag to determine whether to print the first two words. Like so:
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
first = 'They are '
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print(first + str(n), end=' ')
if len(first) > 0:
first = ''
The following solution may help you
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
num = [] # Create empty list
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
num.append(n)
# Write the print statement outside of the loop and use .join() function and for loop
#to print each element of the list look like the output you have posted
#
print('They are'," ".join(str(x) for x in num))
Output:
Displays prime numbers from 1 to N.
Please enter a value of n: 40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37

logic errors with print the sum of all the numbers that are either multiple of three or five

I'm having some logic errors with my program. I've been trying to solve this for the last couple of hours. It's supposed to print the sum of all the numbers that are either multiple of three or five.
my output
1.)enter an integer number (0 to end): enter an integer number (0 to end):
2.)enter an integer number (0 to end): 3+ = 3
expected output
1.)enter an integer number (0 to end): 3 = 3
2.)enter an integer number (0 to end): 3+5 = 8
below is my code.
while True:
answer = ""
num = int(input("enter an integer number (0 to end): "))
end_answer = 0
if num == 0:
exit()
for i in range(1, num+1):
if i%3==0 or i%5==0 :
answer += str(i)
end_answer += i
if i != num and (i%3==0 or i%5==0):
answer += "+"
print(str(answer) + " = " + str(end_answer) )
I've seen similar answers for this just not in python specifically
The following (properly indented) code will give you what you need:
while True:
num = int(input('Enter an integer number (0 to end): '))
if num == 0: exit()
answer = ''
end_answer = 0
sep = ''
for i in range(1, num+1):
if i % 3 == 0 or i % 5 == 0 :
answer += sep + str(i)
sep = ' + '
end_answer += i
if end_answer > 0:
print(str(answer) + ' = ' + str(end_answer) )
Note that it uses a variable separator sep to more cleanly print the item you're working out. A sample run follows:
Enter an integer number (0 to end): 2
Enter an integer number (0 to end): 3
3 = 3
Enter an integer number (0 to end): 10
3 + 5 + 6 + 9 + 10 = 33
Enter an integer number (0 to end): 38
3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 + 20 + 21 + 24 + 25 + 27 + 30 + 33 + 35 + 36 = 329
Enter an integer number (0 to end): 0
You can simplify your code a lot by using the sum builtin and f-strings for printed text formatting. This will likely be more efficient as well.
Code
from itertools import count
counter = count(1)
while True:
num = int(input(f'{next(counter)}). Enter an integer number (0 to end): '))
if num == 0:
break
nums = [x for x in range(1, num + 1) if x % 3 == 0 or x % 5 == 0]
print(f'{" + ".join(map(str, nums))} = {sum(nums)}')
Output
1). Enter an integer number (0 to end): 3
3 = 3
2). Enter an integer number (0 to end): 9
3 + 5 + 6 + 9 = 23
3). Enter an integer number (0 to end): 15
3 + 5 + 6 + 9 + 10 + 12 + 15 = 60
4). Enter an integer number (0 to end): 0

python how to sum odd numbers in a range? [duplicate]

This question already has answers here:
Adding odd numbers in a list
(6 answers)
Closed 5 years ago.
I want to sum my odd numbers, so if I enter num=7 or 8 it counts: 1, 3, 5, 7 : Correct, but I want to sum them. So the answer for 7 and 8 should be 16 (1 + 3 + 5 + 7 = 16)
Or if I enter num=9 then I expect 1 + 3 + 5 + 7 + 9 = 25
I must use While for this calc.
num = int(input("Insert number: "))
sum = 1
num += 1
while sum < num:
print(sum)
sum = sum + 2
You can use the built-in sum() function like this:
num = int(input("Insert number: "))
s = sum(range(1, num+1, 2))
range() takes start (inclusive), end (exclusive), and step (In our case: start=1, end=num+1 and step=2)
Output:
>>> num = 9
>>> s = sum(range(1, num+1, 2))
>>> s
25
If using while is a requirement, then you can achieve the same result with:
>>> s = 0
>>> i = 1
>>> while i < num + 1:
... if i % 2: # Or: i % 2 != 0, which means we only consider odd nums
... s += i
... i += 1
...
>>> s
25

whats wrong with this statement(count from 0-9)

I am trying to get this code to calculate 5 and print numbers by 5's with a while statement of 7, so I want it to loop through, generating a different number 7 times; however, when it gets to a number over 10, I want it to start back over at 0 and ignore the 10.
This is my code:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
The code prints the first number, but freezes on printing any others. Why isn't this working?
Your code is not in the loop. Python's code blocks are created with indents:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
Is this the result you were trying to achieve:
import random
x = random.randint(1,9)
for i in range(1,8):
print x
x += 5
if x >= 10:
x -= 9
This generates a random number, and adds 5 until it passes 10, then subtracts 9, and it does this seven times. If I understand your question correctly, this is what you were trying to do. Please correct me if I am wrong.
no this is not what I was trying to do here is what I am trying to do.
counts = 0
r = 0
firstpickplusfive = 7
p = firstpickplusfive + 5
while r < 3:
firstpickplusfive = p + counts
if firstpickplusfive > 6:
firstpickplusfive = firstpickplusfive + counts - 10
if p > 9:
p = firstpickplusfive + counts
print firstpickplusfive
counts = counts + 1
r = int(r) + 1
it works alone, but when I add it to the script I am trying to write it doesn't work...if there is a simpler way to do it I would appreciate knowing it.
ie.
number = number + 5 + 0
then
number = number + 5 + 1.....ect which
example 7 + 5 + 0 = 12,
7 + 5 + 1 = 13.........
if the number is equal to 10 then I want it to drop the tens place and keep the 1's place
example 7 + 5 + 0 = 2,
7 + 5 + 1 = 3
Here is an easier method:
for i in range(1,4):
num = 7
num += 5
num +=(i-1)
if num >=10:
num -= 10
print num
i+=1
Try this in your script, does it work?

Categories