I am writing some code takes values, the values number
and N
as input and prints, the first N
lines of a multiplication table as below:
3 * 4 = 12
2 * 4 = 8
1 * 4 = 4
What I'd like to do is reverse said output to look like this:
1 * 4 = 4
2 * 4 = 8
3 * 4 = 12
The code is here below. I've thought about using slicing such as [:-1] but I'm not sure how to implement it. Assistance would be appreciated. Thanks.
number = input("Enter the number for 'number ': ")
N = input("Enter the number for 'N': ")
if number .isdigit() and N.isdigit():
number = int(number )
N = int(N)
while int(N) > 0:
print('{} * {} = {}'.format(N,number ,N*number))
N = N - 1
else:
print ('Invalid input')
I would instead recommend using a for loop with the range
method as such:
for i in range(1, N+1):
print('{} * {} = {}'.format(i,number ,i*number)
You could change your while
loop like so:
int i = 0
while i < N:
print('{} * {} = {}'.format(i,number ,i*number))
i = i + 1
Reversing a list is [::-1] (you missed ':') and you are parsing twice the same number N, but in this case you can do
counter = 0
while counter != N:
print('{} * {} = {}'.format(N,number ,N*number))
counter = counter + 1
I think, you can let the program count upwards.
N = int(N)
i = 1
while int(N) >= i:
print('{} * {} = {}'.format(N,number ,N*number)) # TODO: adjust formula
i = i + 1
If you absolutely have to do it using while loops, perhaps something like the following will work.
m = 1
while m <= N:
#Do stuff with m
m += 1
Although I much suggest using a for loop instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.