While loop is not producing multiplication table [duplicate] - python

This question already has answers here:
Python check for integer input
(3 answers)
Closed 2 years ago.
i = 0
d = input("Enter the no. you want ")
while i < 11 :
print(i * d)
i+= 1
It is supposed to give multiplication table of d but it gives the following result for eg. '3' instead
3
33
333
3333
33333
333333
3333333
33333333
333333333
3333333333

The input() returns a string not an int and if you multiply a string in Python with an integer num, then you will get a string repeated num times. For example,
s = "stack"
print(s * 3) # Returns "stackstackstack"
You need to use int() constructor to cast the input from str to int.
d = int(input("Enter the no. you want "))
Try this:
d = int(input("Enter the no. you want "))
for i in range(1,11):
print(i * d)
In the above code, I have replaced your while loop construct with for loop and range() to get sequence of numbers.
BONUS:
To print/display the table in a nice and better way, try the below code:
for i in range(1,11):
print("%d X %d = %d" % (d, i, i * d))
Outputs:
Enter the no. you want 2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20

Related

Find Python Multiplication from user input

Junior Python Coder here! Trying to print multiplication table with user input but stuck.
min_num=1
max_num=11
starting_range = int(input ("Enter the minimum number: "))
ending_range = int(input ("Enter the maximum number: "))
print ("The Multiplication Table of: ", starting_range)
for count in range(1, 11):
print (starting_range, 'x', count, '=', ending_range * count)
if max_num - min_num > 10 :
print('invalid range ')
else:
for num in range (min_num,max_num):
print ("The Multiplication Table of: ", ending_range)
for count in range(min_num, max_num):
print (starting_range, 'x', count, '=', ending_range * count)
I'm not sure I really understand what you're trying to do as your code isn't formatted, but for a multiplication table, a nested loop is a solution.
The basic idea is: For every number in the given range, loop over the whole range and multiply it by each element. This will print the whole multiplication table.
start = 1 # You can change these to be inputted by the user.
end = 10
for i in range(start, end + 1): # We add 1 to the end because range() is exclusive on endpoint.
for j in range(start, end + 1):
print(f"{i} x {j} = {i * j}")
If you only need the table as something like:
15 x 1
15 x 2
15 x 3
...
You can do this with one loop:
num = 10
for i in range(1, num + 1):
print(f"{num} x {i} = {num * i}")
I recommend you search up on F-strings in Python if you do not understand the print(f"..") parts. They're very convenient. Good luck!
Minimal change to original code:
min_num = 1
max_num = 11
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
# Removed the error checking, wasn't sure precisely what it was meant to do.
for num in range(starting_range,ending_range):
print ("The Multiplication Table of: ", num)
for count in range(min_num, max_num):
print (num, 'x', count, '=', num * count)
Try it at https://www.mycompiler.io/view/7JVNtxkT53k
This prints:
The Multiplication Table of: 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
The Multiplication Table of: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The Multiplication Table of: 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Alternative code defining a function:
def print_multiplication_table(n, max_value=11):
if n > max_value or n <= 0:
raise ValueError
print(f"The Multiplication Table of: {n}")
for m in range(1, max_value):
print(f"{n} x {m} = {n*m}")
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
for i in range(starting_range, ending_range):
print_multiplication_table(i)
Try it at https://www.mycompiler.io/view/1uttFLmFEiD

sum of Fibbonaci Sequences?

Trying to add the sum of Fibonacci, using definite loops. It's meant to calculate the summation of Fibonacci number with each number too. Below is the sample for the Fibonacci sequence and its summation, how do i add the sum of the fibonacci eg 1,1,2,3,5,8
Fibonacci Summation
0 0
1 1
1 2
2 4
3 7
5 12
8 20
n = int(input("enter"))
def fibonacciSeries():
a=0
b=1
for i in range (n-2):
x = a+b
a=b
b=x
int(x)
x[i]= x+x[i-1]
#should add the previous sequences
print(x)
fibonacciSeries()
You don't need to keep track of the whole sequence. Plus your Fibonacci implementation doesn't start with 1, 1 but rather 1, 2 so I fixed that.
def fibonacciSeries(n):
a=0
b=1
x=1
series_sum = 0
for i in range (n-2):
series_sum += x
print(f'{x} {series_sum}')
x = a+b
a=b
b=x
n = 10
fibonacciSeries(n)
Output:
1 1
1 2
2 4
3 7
5 12
8 20
13 33
21 54
def fibonacciSeries(n):
sum = 0
a = 0
b = 1
x = 1
sum = 0
for i in range(0,n - 2):
sum += x
print(x,sum)
x = a + b
a = b
b = x
n = int(input("enter : ")) # n = 8
fibonacciSeries(n)
Output:
enter : 8
1 1
1 2
2 4
3 7
5 12
8 20

For loop a square pattern of numbers

In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()

Multiplying items in a range by an integer in Python

I'm very new to stack overflow and python so please bear with me. I am trying to write a code that will output:
0 x 8 = 0
1 x 8 = 8
2 x 8 = 16
3 x 8 = 24
4 x 8 = 32
5 x 8 = 40
6 x 8 = 48
7 x 8 = 56
8 x 8 = 64
9 x 8 = 72
I have searched the internet and tried the following:
x=(int(z)*8 for z in range(10))
print(str(z) + 'x 8 = ' + str(x))
Which prompts error messages:
"TypeError: can only concatenate list (not "str") to list"
"NameError: name 'z' is not defined"
I have tried this using 'item' instead of 'z', with no luck.
I know this is a very basic task but all solutions I can find online are the same as the invalid code I already have. Thank you for any help you can offer.
Try this:
x=(int(z)*8 for z in range(10))
print("\n".join(str(z) + ' x 8 = ' + str(y) for z, y in zip(range(10), x)))
x is the list of 8 multiplications. You then iterate over the list using a for loop and get every pair using the zip() function. You then join it with \n designating newlines.
The issue is that the variable z is only defined on your first line (within the context of the loop) and is not accessible to the other lines of your code. Instead you will want to loop through the values and perform the operation and printing on each item.
for z in range(10):
print '{0} x 8 = {1}'.format(z, z * 8)
# 0 x 8 = 0
# 1 x 8 = 8
# 2 x 8 = 16
# 3 x 8 = 24
# 4 x 8 = 32
# 5 x 8 = 40
# 6 x 8 = 48
# 7 x 8 = 56
# 8 x 8 = 64
# 9 x 8 = 72
The other alternative is to store the multiplication results in x (as you have already done) and then loop through that to display each of them using enumerate to get the index.
x = [z * 8 for z in range(10)]
for k,value in enumerate(x):
print '{0} x 8 = {1}'.format(k, value)

creating a multiplication table in python with user input [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am extremely new to python so my question might seem a bit too trivial but believe me, its like rocket science right now for me. So, here it is. I have to create a program that reads single line of user input containing an integer, and print out the multiples of that number up to 12 times that number. For example:
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
I think I have to use for and range functions but don't know how to do that. Please help me :(
This is a really basic program,, all the above things are covered in basic python itself..
You should go through the following links like Bytes of Python, LearnPython etc
There are more tutorials for you to go through and get started..
This line takes the input from the user
In [8]: n = raw_input("Enter a number : ")
Enter a number : 5
This line iterates over the set of values 1 to 12, in python we can use range function for this..
range(1, 13) is there coz it range function stops before the last value..
%s is a string formatting syntax, similar to C, finally int(n) is coz inputs are in string format, need to convert it to int before multiplication can happen
In [9]: for i in range(1,13):
...: print "%s x %s = %s" %(n, i, i*int(n))
...:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
>>> inp=int(raw_input('enter an integer'))
enter an integer 5
>>> for i in xrange(1,13): #use range() if you're on python 3.x
... print "{0} X {1} = {2}".format(inp,i,inp*i)
...
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
5 X 11 = 55
5 X 12 = 60
var = raw_input("Enter something: ")
print "you entered ", var
for n in range(1, 10):
prod = int(var)*n
print str(var) + " X " + str(n) + " = " + str(prod)
x = raw_input('Enter a number: ')
for i in range(1,12):
print int(x)*i
But you should check if x is really a number ;)
And here's an incomprehensible oneliner that will fit in a Tweet, because why not:
import itertools;print"\n".join("%d x %d = %d"%(x+(x[0]*x[1],))for x in itertools.product([int(raw_input("Enter a number : "))],range(1,13)))

Categories

Resources